简体   繁体   中英

JQuery validate field number length depends

I'm using JQuery Validation Plugin and i want change the minlength and maxlength of my field (zip code) that depends if country is equal to PT (Portugal) or not.

But in validation the result are always:

在此处输入图片说明

Anyone knows what is my problem?

HTML:

<body>
<form id="myform">
<label for="field">Required, minimum 3(Other) or 9(PT): </label>
    <input type="text" class="left" id="field" name="field"/>
<br/>
<select class="form-control" id="country" name="country">
    <option value="0">-- Choose Country --</option>
    <option value="BE">Belgium </option>
    <option value="BR">Brazil </option>
    <option value="ES">Spain </option>
    <option value="FR">France </option>
    <option value="NL">Dutch </option>
    <option selected="selected" value="PT">Portugal </option>       
    </select>    
    <br/>
    <input type="submit" value="Validate!"/>
</form>
</body>

JQuery:

jQuery.validator.setDefaults({
    debug: true,
    success: "valid"
});

$("#myform").validate({
    rules: {
        field: {
            required: true,
            number: {
                depends: function () {
                    return !($('select[name="country"]').val() != 'PT');
                }
            },
            minlength: {
                depends: function () {

                    var country = $('select[name="country"]').val();

                    if (country == 'PT') {
                        return 9;
                    } else {
                        return 3;
                    }
                }
            },
            maxlength: {
                depends: function () {
                    var country = $('select[name="country"]').val();
                    if (country == 'PT') {
                        return 9;
                    } else {
                        return 50;
                    }
                }
            }
        }
    }
});

JSFIDDLE

Answer Updated.

I have made some changes, removed depends .

Here is the updated fiddle. http://jsfiddle.net/Y2H9d/36/

$("#myform").validate({
    rules: {
        field: {
            required: true,
            number: function () {
                return !($('select[name="country"]').val() != 'PT');
            },
            minlength: function () {
                var country = $('select[name="country"]').val();
                if (country == 'PT') {
                    return 9;
                } else {
                    return 3;
                }
            },
            maxlength: function () {
                var country = $('select[name="country"]').val();
                if (country == 'PT') {
                    return 9;
                } else {
                    return 50;
                }
            }
       }
    }
});

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