Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call JQuery Validate Plugin without submitting the form

Is it possible to validate the form without submitting it using the JQuery validation plugin?

I use a button.click function for validating, the button isn't a submit input.

like image 420
lady_OC Avatar asked May 23 '13 23:05

lady_OC


People also ask

Does jQuery validate require a form?

The jquery validate plugin requires a form element to function, so you should have your form fields (no matter how few) contained inside a form. You can tell the validation plugin not to operate on form submission, then manually validate the form when the correct submit button is clicked.

How Prevent form submit in jQuery validation fails?

You need to do two things if validation fails, e. preventDefault() and to return false. This really works.

How can I disable enable submit button after jQuery validation?

You can only disable it after the form has successfully passed validation. Use the plugin's submitHandler option for this as it's fired on a button click only when the form has passed validation. $(). ready(function() {... is not recommended as per jQuery documentation.


1 Answers

Yes, it is possible to test the form's validity without submitting it.

See .valid() method.

Whenever .valid() is called the form is tested, and the method will also return a boolean.

$(document).ready(function () {

    $('#myform').validate({ // initialize the plugin
        // rules & options
    });

    $('#button').click(function() {
        if ($('#myform').valid()) {
            alert('form is valid - not submitted');
        } else {
            alert('form is not valid');
        }
    });

});

Working DEMO: http://jsfiddle.net/zMYVq/

like image 83
Sparky Avatar answered Sep 29 '22 12:09

Sparky