I need to validate a form with jQuery. I can check all my inputs one by one, but it's not a very practical solution.
How can i check if all my inputs are non-empty more efficiently? In my form i can have input
elements of different types: text, several groups of radio, select etc.
You can do it.here is code
I just wanted to point out my answer since I know
for
loop is faster then$.each
loop here it is:just add
class="required"
to inputs you want to be required and then in jquery do:you might want to wrap this in a keyup function
Like this:
Just use:
If it's zero, none are empty.
To be a bit smarter though and also filter out items with just spaces in, you could do:
Use
each
:However you probably will be better off using something like
jQuery validate
which IMO is cleaner.