Reputation: 10586
How to validate form fields on submit button using jquery?
Upvotes: 3
Views: 545
Reputation: 35679
jQuery.validate plugin is the best solution I've found.
All you have to do is give the inputs the class "required" and then call .validate() on the form in your document.ready call e.g.
$(function() {
$('#idofyourform').validate();
});
You can also do email and other types of validation using the same method. The plugin is also extensible for your own rules.
Remember you will also need to validate any data server side.
Edit: Response to comment
To validate only when the form is submitted add the following option:
$("#idofyourform").validate({
onsubmit: true,
onkeyup: false,
onfocusout: false,
onclick: false
})
Have a read of the options for more detail.
Upvotes: 3