Form validation using the keyboard events
Using the onkeypress event, we could create on-the-fly form validation. Now that's something CGI most certainly can't do. The idea is to use the onkeypress event handler inside the form field to check, so as the user types, it is being validated concurrently.
Here's an example that limits a form field to contain only numeric input (on the fly, of course!).
Try typing in stuff other than numbers into the above box- you can't. How does the script work? By checking to see whether the keycode of the key pressed as the user types falls within the range of the number keys (48~57), and if not, returns false, which disables the keypress action. How did I know 48 to 57 represents the keycode of the numeric keys? Well, by using a simple alert(event.keycode) script to map it out (no fancy tricks here). Then, by using the "onkeypress" event handler, along with function numbersonly() inside the textbox to validate, we have one picky box!
Limit number of characters allowed
Enter text (max length is 20 characters):
Short and effective way to limit the amount of text inside form fields!