Validate form in Click event using jQuery validator

I am using jQuery validation to validate input. My code is:

 $('#button').click( function() {       
    $("#form").validate({              
        rules: {
            phone: {
                required: true,
                number: true,
                rangelength: [7, 14]
            }
        }
    }); 
});

And HTML:

<form id="form" name="form" method="post" action="">
 <input id="phone" name="phone" class="required" type="text" />
 <div class="button" id="send_b">Send</div>
 </form>

This code does not work. If I add this jQuery line

$("#form").submit();  

inside the click event it works. But I don’t want to submit the form, so I just want to do a check by clicking.

Does anyone know how to do this?

+5
source share
3 answers

Just add .form()to manually manually run the test (the default behavior expects a send event):

$('#button').click( function() {       
  $("#form").validate({              
    rules: {
      phone: {
        required: true,
        number: true,
        rangelength: [7, 14]
      }
    }
  }).form(); 
});
+13
source

click . , , " , ".

( , ). validation() form; submit.

, click.

0

form() click

0

All Articles