JQuery validation without form tag

I have a β€œform” with many inputs, and on the submit button, I call the javascript function, which manages the information and sends ajax to the php file.

Can I add a form tag without an action url to validate my "form" using jquery validation? Or should I do a manual check?

Thanks!

+8
jquery jquery-validate
source share
1 answer

"I have a" form "with many inputs, and on the button" Submit "I call javascript, which manipulates the information and sends ajax to php File"

It is absolutely imperative that you have <form></form> tags for the jQuery Validate plugin to function properly or in general.

If you use ajax , you must put your ajax inside the submitHandler the jQuery Validate plugin . Below is my example.

"Can I add a form tag without an action URL to validate my" form "using jquery validation?"

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

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

Put your ajax and return false in submitHandler and no regular submitHandler will be executed:

 $(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

According to the OP comments, to use input type="button" instead of input type="submit" , you need to use a click handler. No need to have built-in JavaScript. Remove the entire onClick function.

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

+5
source share

All Articles