Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery validation without form tag

I have a "form" with many inputs and at the submit button I call a javascript function that manipulate the info and send by ajax to a php file..

I can add a form tag without an action url to validate my "form" with the jquery validate? Or I have to do manually the validation?

Thanks!

like image 724
Diego_sf93 Avatar asked Mar 05 '13 18:03

Diego_sf93


1 Answers

"I have a 'form' with many inputs and at the submit button I call a javascript function that manipulate the info and send by ajax to a php file"

It is absolutely required that you have <form></form> tags for the jQuery Validate plugin to function properly, or at all.

If you use ajax, you must put your ajax inside the submitHandler of the jQuery Validate plugin. See below for my example.

"I can add a form tag without an action url to validate my "form" with the jquery validate?"

Yes, you can remove or block the action and still use jQuery Validate.

See demo: http://jsfiddle.net/NEPsc/3/

Put your ajax and a return false in the submitHandler and no regular submission will occur:

$(document).ready(function() {

    $('#myform').validate({ // initialize plugin
        // your rules & options,
        submitHandler: function(form) {
            // your ajax would go here
            return false;  // blocks regular submit since you have ajax
        }
    });

});

EDIT:

As per OP's comments, to use a input type="button" instead of a input type="submit", you'll need to use a click handler. There is no need to have any inline JavaScript. Remove the onClick function entirely.

See updated demo: http://jsfiddle.net/NEPsc/5/

like image 147
Sparky Avatar answered Oct 18 '22 16:10

Sparky