I am using jQuery validate for validate an input. My code:
$('#button').click( function() {
$("#form").validate({
rules: {
phone: {
required: true,
number: true,
rangelength: [7, 14]
}
}
});
});
And the 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 is not working. If I add this line of jQuery
$("#form").submit();
inside the click event it works. But I don't want to submit the form so I want just to make the validation on click.
Does anyone know how to do this?
$(document). ready(function(){ $("#form1"). validate({ rules: { field1: "required" }, messages: { field1: "Please specify your name" } }) $('#btn'). click(function() { $("#form1").
$("#form_id"). valid(); Checks whether the selected form is valid or whether all selected elements are valid. validate() needs to be called on the form before checking it using this method.
Inside the document. ready event handler, the Validate Button has been assigned with a Click event handler. When the Validate Button is clicked, first the TextBox is referenced using jQuery and then its value is trimmed and compared with an Empty string.
validator. addMethod( name, method [, message ] ) Description: Add a custom validation method. It must consist of a name (must be a legal javascript identifier), a javascript based function and a default string message.
Just add .form()
to manually trigger the validation immediately (the default behavior waits for a submit event):
$('#button').click( function() {
$("#form").validate({
rules: {
phone: {
required: true,
number: true,
rangelength: [7, 14]
}
}
}).form();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With