views:

284

answers:

3

I have a registration form which I need to validate before submit. The form has the following fields:name,email, and password. I need the name to have a value, the email to have the correct format, and the password to be at least 6 characters.

What is the best way to do all this validation and hightlight incorrect fields live before submit?

Thanks, Brad

+3  A: 

If you're using jQuery, you may want to look into a jQuery validation plugin. This one seems to be popular, and looks pretty nice. If you're not using jQuery, you probably should be. ;)

B.R.
I agree with B. You should check into JavasScript and if you don't want to reinvent the wheel, you should check into jQuery, which will do most work for you.
Gert
+2  A: 

Hope this isn't too off topic, but I've have to say that tackling this problem solely in JavaScript is never going to completely solve the problem, in the main due to the fact that it's trvial to stomp all over whatever logic you try and impose using browser developer tools, etc. (You also can't presume that everyone has JavaScript enabled, etc.)

As such, whilst you can carry out some nice real-time validation, etc. using JavaScript (with jQuery for nice visual effects, etc.) you'll need to carry out the real validation in whatever server side environment you're using.

In terms of the JavaScript 'validation' itself, simply attach functions that update the appropriate parts of the DOM with success/failure messages to the onchange, etc. events of the relevant inputs. See http://www.w3schools.com/jS/js_form_validation.asp for the basics.

middaparka
A: 

jQuery is obviously a great lib. I've found jQuery validation to be decent, but I wound up rolling my own helper lib because I needed a sleeker look, and a leaner syntax. I put it up on Google code for anyone who can find use from it. It's worked well for me.

http://code.google.com/p/javascript-validation-form/

jamisonLikeCode