我试图将自定义验证方法添加到jquery验证插件。当我尝试将规则附加到元素时,我收到错误“uniqueTicket”未定义。
这是我的添加方法:
jQuery.validator.addMethod("uniqueTicket", function (value, element, param) {
var data = {
ticket: value,
reservation_id: $("#reservation_id").val()
};
if (value.length > 3) {
$.ajax({
type: "POST",
url: '/ticketaudits/validateTicket',
data: data,
dataType: "json",
success: function (msg) {
//If username exists, set response to true
response = (msg == 'true') ? true : false;
}
});
} else {
response = false;
}
return response;
},
"Ticket is invalid or already used");
在与验证相同的文档就绪函数中调用它。我的验证是:
$('#myForm').validate({
debug: false,
ignoreTitle: true,
onkeyup: false,
rules: {
ticket: uniqueTicket
}
});
我已经遵循了一些例子,但不明白这个问题。这是否意味着我的addmethod出现了错误?
答案 0 :(得分:2)
假设您的input
包含name="ticket"
,
这样:
rules: {
ticket: uniqueTicket
}
应该是这样的:
rules: {
ticket: {
uniqueTicket: true
}
}