简体   繁体   English

jQuery验证字段号长度取决于

[英]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. 我使用jQuery验证插件 ,我想改变minlengthmaxlength取决于国家是否等于PT(葡萄牙),或不是我的领域(邮政编码)。

But in validation the result are always: 但是在验证中,结果始终是:

在此处输入图片说明

Anyone knows what is my problem? 有人知道我有什么问题吗?

HTML: 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的:

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 的jsfiddle

Answer Updated. 答案已更新。

I have made some changes, removed depends . 我已经做了一些修改,删除depends

Here is the updated fiddle. 这是更新的小提琴。 http://jsfiddle.net/Y2H9d/36/ 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;
                }
            }
       }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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