How to validate form fields on submit button using jquery?
+2
A:
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.
Rob Stevenson-Leggett
2010-07-22 21:54:39
I know that one ..but how do I use that to validate only when form is submitted.
yogsma
2010-07-22 21:55:23
Do you mean you don't want inline validation? Only when the user clicks submit? If so see expanded answer.
Rob Stevenson-Leggett
2010-07-22 22:00:27
Somehow it is not working.
yogsma
2010-07-23 14:10:01
Is there an error?
Rob Stevenson-Leggett
2010-07-23 16:13:51
Ah, sorry I've misremembered this. It's the other way around. I've editted my code to reflect this.
Rob Stevenson-Leggett
2010-07-23 16:15:50
It doesn't throw any error ..here is my code$('#formname').validate({onsubmit:true,onkeyup:false,onclick:false});
yogsma
2010-07-23 16:42:33
You haven't got onfocusout:false is it still validating when you leave a textbox?
Rob Stevenson-Leggett
2010-07-24 09:10:03
I added that too. It doesn't validate in any way. Do you have a working example?
yogsma
2010-07-26 20:21:02
You do have the class "required" or "email" on the fields?
Rob Stevenson-Leggett
2010-07-26 20:48:46
Yes, I do have class "required" on the fields
yogsma
2010-07-28 15:42:27