Jquery validation plugin | resetForm is not working

前端 未结 4 2109
小鲜肉
小鲜肉 2020-12-30 07:44

I am trying to clear all error messages as well error highlighting when user click on the clear form button, below are actual requirements

  1. Validat
4条回答
  •  悲&欢浪女
    2020-12-30 08:31

    $("#register_form")[0].reset();, form fields are getting clear, but error messages are not getting cleared.

    to do this you can put one more line below it:

    function clearInputForm(){
       alert("");
       var validator1 = $( "#register_form" ).validate();
       validator1.resetForm();
       $("#register_form")[0].reset();
       $('.error').hide();
    }
    

    Although you should do this way:

    $( document ).ready(function(){
       var validator = $("#register_form").validate({
           focusCleanup: true,
           rules: {
             // custom rules
           },
           messages: {
             // custom messages
           }
        });
        $('[type="reset"]').on('click', function(){
           validator.resetForm();
        });
    });
    

    You should put your validator.resetForm(); in the click event of reset button.

提交回复
热议问题