jQuery Validation Plugin: How can I force validation on previously valid fields?

前端 未结 2 756
Happy的楠姐
Happy的楠姐 2021-02-06 01:09

I\'m using jQuery Validation plugin to validate a form. Do you know how to force revalidation on previously successful fields?

I have tried the .form fu

相关标签:
2条回答
  • 2021-02-06 01:55

    I came upon this answer when I was looking into using jQuery remote unobtrusive validation. It wasn't the best documented topic in the world and also it wasn't without quirks. So much so that I ended up writing a blog post about what I learned. Link here in case helpful:

    http://icanmakethiswork.blogspot.com/2012/03/jquery-unobtrusive-remote-validation.html

    0 讨论(0)
  • 2021-02-06 02:01

    The state of validation for remote fields is stored via $.data() with the element you want to validate, so you could use .removeData() to clear that out...so it's forced to revalidate:

    $("#form1 :input").removeData("previousValue");
    //now call .valid()
    

    This forces the check if the value has changed (we need to re-validate) to be true:

    //This code is in the validation plugin for remote:
    var previous = this.previousValue(element);
    if (previous.old !== value) { //this is normally false, since it hasn't changed
    

    If there are only specific fields that need re-validating, like you said username, you may want to narrow the $("#form1 :input") selector to only the fields you want, to make it a bit more efficient.

    0 讨论(0)
提交回复
热议问题