Is there a function in jquery.validate that will reset a single field like resetForm does for a form?

后端 未结 6 2351
旧巷少年郎
旧巷少年郎 2021-02-18 15:59

I want to call a jquery function to manually remove errors from a single field and reset the error markup. Is there a function that does this that would be similar to the reset

相关标签:
6条回答
  • 2021-02-18 16:03

    This functionality is possible via focusCleanup: true option and triggering focusin event on desired element. But it is not always desirable to trigger this event. So apparently, you can use plugin’s internal code which is running on focusin and invoke it externally like so:

    /*get your element and containing validated form*/
    var $that = $('#desired_elemenet');
    var $form = $that.closest('form');
    
    /*get validator object for the form*/
    var validator = $form.validate();
    
    /*use internal code of a plugin to clear the field*/
    if (validator.settings.unhighlight) {
        validator.settings.unhighlight.call( validator, $that[0], validator.settings.errorClass, validator.settings.validClass );
    }
    validator.hideThese( validator.errorsFor( $that[0] ) );
    
    0 讨论(0)
  • 2021-02-18 16:10

    Can't find anything built in to the validation library but this hack works:

    $('#email').removeClass('error').next('label.error').remove();
    
    0 讨论(0)
  • 2021-02-18 16:15

    You can do the following to validate a single field:

    $('#your_field').valid();
    

    Maybe it will help someone :-)

    Thanks!

    0 讨论(0)
  • 2021-02-18 16:19

    this resets the entire form

    var validator = $("#myform").validate();
    validator.resetForm();
    

    this will reset only the name element

    $("#skip").click(function() {
       var rules = $("#name").removeAttrs("min max"); // remove min and max
       $("#form").submit(); // submit, as there are no attributes, it will not have an error
       $("#name").attr(rules); // add it again so you can validate it
    });
    
    0 讨论(0)
  • 2021-02-18 16:24

    you can use

    $("#selector").removeClass("error");
    $("#selector-error").remove();
    
    0 讨论(0)
  • 2021-02-18 16:27
    var form = $('#form'); 
    var validator = form.validate();
    
    // Validate single field from that form.
    var myfield = $('#myfield');
    myfield.removeData('previousValue').removeAttr('aria-invalid');
    validator.resetElements(myfield);                         
    
    0 讨论(0)
提交回复
热议问题