Question

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

Was it helpful?

Solution

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. ;)

OTHER TIPS

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.

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/

Jquery validation isn't enough, you need server side validation, anyone can disable javascript.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top