开发者

jQuery Validate and Ajax - resetting validation after fail

开发者 https://www.devze.com 2023-04-08 06:02 出处:网络
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

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: func开发者_开发知识库tion(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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消