简体   繁体   中英

using depends with max - jquery validation

I need to add a conditional max value on a field using jQuery validation . I learned about depends but somehow I think the validate is not working.

This is the block of code. The dropdown selection is working properly. I had placed an alert and it gives the desired value. However, the max validation doesn't work.

rules: {
   txtFirstName: { required: true,
     max: { depends: function (element) {
              var dropdown1 = $("#ddl option elected").val();
              if (dropdown1 == "C") {
                 return 100;
              }
              else {
                 return 200;
              }
}

What am I doing wrong here? Or am I missing something?

@Spanky answer is correct. but inorder for it to work, You need to add param

        max: {
            param: 6.2 // Max value
            depends: function (element) {
                return if(10>2); //Condition
            }
        }

Fiddle

You cannot return a parameter using the depends method.

Here's a quick test that shows how 200 is interpreted as a boolean true ...

$('form').validate({
    rules: {
        txtFirstName: {
            required: true,
            max: {
                depends: function (element) {
                    return 200;
                }
            }
        }
    }
});

http://jsfiddle.net/pf6tpamp/

The depends method is for rules that can only be declared with a boolean true and no parameters.

(Also, max is looking for numerical input. Maybe you wanted maxlength ?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM