Form Input Validation With Javascript

June 5, 2009    client side scripting input checking javascript reduce server load

Input validation with javascript is an essential part of any large day to day website. Its major advantage is that it can be processed client side thus avoiding avoidable load on the server.
If your running a PHP based website which is struggling to cope with load and your using basic php form validation. You could switch your form validation to Javascript.

You should treat your javascript validation as your first line of defence, as javascript source can be read by a user its easy enough to work around it if you know what your doing. So you will still need to keep your second line of defence ie your PHP code to do the second lot of checking.

However the javascript will help to limit load for people who genuinely make honest mistakes.

Heres a example of form validation.

Firstly lets make our Form

[sourcecode='html']

Whats your name:   



[/sourcecode]

This will simply check to make sure they have not tried to submit the form without entering anyting in the input box.

This is a very very simple version of a javascript validation.

[sourcecode='javascript']

[/sourcecode]

Here we have added another line of validation to check to see they at dont try and just put one character in the box using javascripts .length function.

I will add some more to this post later, including email validation and more.


blog comments powered by Disqus