【发布时间】:2015-02-08 13:06:41
【问题描述】:
我对 jQuery Validation 非常陌生,请有人纠正我在尝试添加的规则中出错的地方。
<script>
$("#form").validate({
focusInvalid: false,
rules: {
ownership: {
required: true
},
vin: {
required: true,
validateVin: true
},
// Same for other fields
},
messages: {
ownership: "This field is required.",
vin: "Invalid VIN",
// Repeat for other fields
}
});
// create your custom rule
jQuery.validator.addMethod(validateVin(this.value, Number($("#vehicleyear").val()))) {
function validateVin(vin, date) {
var re;
if (date >= 1981) {
re = new RegExp("^[A-HJ-NPR-Z\\d]{8}[\\dX][A-HJ-NPR-Z\\d]{2}\\d{6}$");
} else if (date < 1981) {
re = new RegExp("^[A-Z\\d]{2,17}$");
} else {
}
return vin.match(re);
}, 'Please enter valid VIN.'});
</script>
这应该检查两个字段,首先检查选择vehicleyear 的年份,然后根据年份检查输入vin 以查看正则表达式是否匹配。如果它不正确匹配,那么它应该说无效 vin。
当我运行我的时,它甚至不使用正则表达式,但我不知道为什么。对此的任何帮助将不胜感激!
【问题讨论】:
-
检查你的控制台(chrome - f12 - console)你可能有一些错误。
标签: javascript jquery jquery-validate