我试图动态地添加一个jQuery验证器规则,但它不起作用。它不会抛出任何错误,但不会触发验证。
这是js:
$(document).ready(function () {
$.validator.addMethod("TIME", function (value, element) {
var re = new RegExp(regexp);
console.debug("Validating " + value);
return this.optional(element) || re.test("^*(1[0-2]|[1-9]):[0-5][0-9] *(a|p|A|P)(m|M)*$").test(value);
}, "Time is invalid: Please enter a valid time.");
$("input[id *= '_timepicker']").rules("add", "TIME");
});
你知道怎么回事吗?
编辑:
现在我得到了一个错误:无效量词*(10-2\1-9):0-5
以下是相关的js:
rules:{"special.Hours.FridayHours1.close":{ regExp: /^*(1[0-2]|[1-9]):[0-5][0-9] *(a|p|A|P)(m|M)*$/ }
以下是完整的验证功能:
http://pastebin.com/YRday2Mv
发布于 2010-06-04 02:52:38
语法关闭了一点,您需要传递一个对象而不是字符串,如下所示:
$("input[id*='_timepicker']").rules("add", {"TIME": true });
You can find the .rules()
documentation with more examples here。
发布于 2013-09-23 15:14:53
$(function() {
$('#order_form_id').validate();
$("input[id*='_quantity']").each(function () {$(this).rules("add", {required:true,zznum:true});})
});
https://stackoverflow.com/questions/2971200
复制相似问题