0

I have been trying to get this working for longer than i probably should have, and after reading numerous SO threads on this, i still cant figure it out. Could someone please tell me what I am doing wrong.

I am trying to setup a dynamic rule in jquery validation which will set an input text box minimum value = 1000 if my debitorder check box is checked, but if both the debitorder and my other checkbox are checked, then the minimum value should be 500. below is my latest attempt of many, but i seem to just break the validation completely every time.

amount: {
                required: "#debitorder:checked",
                number: true,
                minValue: function() {
                    if ($("#othercheckbox").is(":checked")) {
                        minimum: 500
                    } else {
                        minimum: 1000
                    }
                }


            },
Fizor
  • 1,480
  • 1
  • 16
  • 31
  • Maybe try the `if` with `$("#othercheckbox").hasAttr("checked")`? – Filnor Dec 01 '17 at 07:42
  • @chade_ `jQuery` doesn't have `hasAttr()` function – pmaddi Dec 01 '17 at 07:51
  • 1
    @pmaddi Sorry, my fault. We have custom jQuery extensions in our comapny with that function. But I found an [answer that might help](https://stackoverflow.com/a/1318091/4733879) – Filnor Dec 01 '17 at 07:54

2 Answers2

0

Is your if conditions working properly? If yes, try

amount: {
    required: "#debitorder:checked",
    number: true,
    minValue: function() {
      if ($("#othercheckbox").is(":checked")) {
        return 500;
      }
      else {
        return 1000;
      }
    }
},
pmaddi
  • 449
  • 5
  • 18
  • thanks, this put me on the right path. I had to return the following though for it to work `return { minimum: 500 }` – Fizor Dec 01 '17 at 08:25
-1

If you are using jquery validation plugin you can do it in 2 ways, one is out of the validation object, jquery validation work together the html attribute or html class value to define regex methods the are defined in the file additional-method, for the minlength attribute you can write out of the jquery validation object on the click checkbox or document ready where you need, you change the value of the minlength with jquery:

                if ($("#othercheckbox").is(":checked")) {
                    $('[name=amount]').attr('minlength',500)
                } else {
                    $('[name=amount]').attr('minlength',1000)
                }

Or you can do it inside the minlength on the jquery validation object in this way, not minValue but minlength and return the value

           minlength: function() {
                if ($("#othercheckbox").is(":checked")) {
                   return 500
                } else {
                    return 1000
                }
            }

For required true o false in dependence of another input value you must use the depends functions for example

  myinputName:
      required:{
          depends : myDependency
      }

Out of the jquery object you define the function so

      myDependency = function(){
            if($("#othercheckbox").is(":checked")){
                   return true;
            }
            else{
                return false;
            }
      }

Ciao