gpt4 book ai didi

javascript - JQuery 验证字段数长度取决于

转载 作者:行者123 更新时间:2023-11-30 17:22:34 26 4
gpt4 key购买 nike

我正在使用 JQuery Validation Plugin我想更改我的字段(邮政编码)的 minlengthmaxlength,这取决于国家/地区是否等于 PT(葡萄牙)。

但在验证中,结果总是:

enter image description here

谁知道我的问题是什么?

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

最佳答案

答案已更新。

我做了一些更改,删除了depends

这是更新后的 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;
}
}
}
}
});

关于javascript - JQuery 验证字段数长度取决于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24863903/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com