Apr
Dynamic JavaScript Form Validation
Posted by Michael in Design, JavaScript
This lightweight JavaScript form validation allows you to easily add attractive validation messages to your forms. There is no markup to add on your existing page. Just call the inlineMsg() function when you wish to display a message. The position of the form element (or any other element) is dynamically calculated.
Call the function as follows…
inlineMsg('name','You must enter your name.',2);
The first parameter is the id of the element you wish to display the warning in relation to. The second is the string you wish to display in the message box. You can include HTML, just be sure and escape when necessary. The third parameter is the time to display the message in seconds. It is optional and if left empty will default to the MSGHIDE variable in the JavaScript which is initially set to 3.
This has been tested working in IE6/IE7/IE8, Firefox, Opera and Safari. Please report any bugs to michael@leigeber.com.
Click here to download the source.
Update 5/5/2008 - Updated the script to focus to the erroneous textbox when displaying an alert. Thanks to mbh for the suggestion.
Update 6/2/2008 - Resolved issue with form field focus on non textbox fields. Added select dropdown to the demo and source.
Excellent work - very useful.
Ian Beadle on 01 May 2008 at 2:15 amMany thanks