我需要对邮政编码进行基本验证,如果为空则显示错误,如果匹配给定值,也会显示错误。
var zip_cde = "1234,5095,2356";
postcode: {
required: true,
number: true,
minlength: 4
},
<input type="text" name="postcode" id="postcode" placeholder="5095" >
我希望如果postcoe匹配 zip_cde ,jquery验证器将显示错误。
非常感谢任何帮助。
答案 0 :(得分:0)
var zip_cde = "1234,5095,2356";
postcode: {
required: true,
number: true,
postalcode: true
},
jQuery.validator.addMethod("postalcode", function(postalcode, element) {
return this.optional(element) || (postalcode.match(/^([0-9]{4})$/) && $.inArray(postalcode, zip_cde));
}, "Please enter a valid postcode");
<input type="text" name="postcode" id="postcode" placeholder="5095" >