jQuery Validate 和 Ajax - 失败后重置验证
我有一个使用 jQuery 和验证插件的表单。其中一个元素使用 Ajax 来检查用户名是否正在使用。当使用已知正在使用的用户名进行测试时,它可以正常工作并突出显示表明用户名正在使用的字段。但是,当我将用户名更改为未使用的用户名时,它永远不会清除错误消息并重新验证。
$("#frmID").validate({
onkeyup:false,
highlight: function(element, errorClass) {
$(element).addClass('inputError');
},
unhighlight: function(element, errorClass) {
$(element).removeClass('inputError');
},
rules: {
username :{
required: true,
usernamecheck: true
}
然后用户名检查功能是:
$.validator.addMethod('usernamecheck',function(username) {
var result = true;
var postURL = compath + "/user.cfc?method=QueryUserExists&returnformat=plain&";
$.ajax({
cache:false,
async:false,
type: "POST",
data: "username=" + username,
url: postURL,
success: function(msg) {
result = (msg=='FALSE')? true : false;
}
});
return result;
},'');
为了清楚起见,我省略了验证错误消息和其他一些验证。
I have a form using jQuery and the Validation plugin. One element uses Ajax to check if a username is in use. When testing with a username known to be in use, it works correctly and highlights the field stating the username is in use. However, when I change the username to one that is not in use, it never clears the error message and revalidates.
$("#frmID").validate({
onkeyup:false,
highlight: function(element, errorClass) {
$(element).addClass('inputError');
},
unhighlight: function(element, errorClass) {
$(element).removeClass('inputError');
},
rules: {
username :{
required: true,
usernamecheck: true
}
and then the username check function is:
$.validator.addMethod('usernamecheck',function(username) {
var result = true;
var postURL = compath + "/user.cfc?method=QueryUserExists&returnformat=plain&";
$.ajax({
cache:false,
async:false,
type: "POST",
data: "username=" + username,
url: postURL,
success: function(msg) {
result = (msg=='FALSE')? true : false;
}
});
return result;
},'');
I've left out the validation error messages and some other validation for clarity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试删除突出显示和取消突出显示方法,并在验证设置中简单地指定 errorClass 选项。由于您所做的只是添加和删除类,因此您可以让验证框架为您完成此操作。您可以在此处查看有关 errorClass 选项的更多信息: http://docs.jquery.com/Plugins /验证/validate#options
I would try removing your highlight and unhighlight methods and simply specify an errorClass option in your validation setup. Since all you are doing is adding and removing a class you can let the validation framework do this for you. You can see more about the errorClass option here: http://docs.jquery.com/Plugins/Validation/validate#options