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

前端 未结 2 755
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 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.

提交回复
热议问题