HTML Form code Auto Generation with Javascript Validation
There are tons of websites that generate HTML form code but probably only a few that do it with client-side Javascript validation code that is executed before the form is submitted.
FormGenics.com 's HTML Form generator not only creates professional looking forms for free, but does so with Javascript validations such as:
- Required Field Validation
- Email Address Format Validation
By default, the validation code generates a pop-up alert but this can be easily modified to display error messages by manipulating the Document Object Model (DOM).
Modifying the generated Formgenics form in order to display error message on the webpage, rather than as a pop-up alert
Example of Auto-generated code for an Email textbox
|
There are 3 changes that will have to done:
1) Insert a <div> element at the location marked (1) in the generated code above. This is what the element will look like,
<div id='ErrorMessages'> </div>
2) Replace all the '\n' s in the Javascript code with <br/> 's . For example, in the line marked by (2), the code will be replaced as follows:
msg+='- Invalid Email Address: '+email+'<br/><br/>';
3) Modify the line marked by (3), by replacing the alert() function call with the DOM manipulation code that changes the contents of the <div> element we added in step 1. For example, the code in this example will be modified as:
document.getElementById('ErrorMessages').innerHTML = 'The following fields are empty or invalid:<br/><br/>'+msg;
Make sure both the opening round bracket '(' in the beginning and the closing round bracket ')' near the end are removed.
Now the error message instead of popping up will be printed in the page itself at the spot where the <div> element was added. You can add CSS styles to the div element to increase/decrease the font-size of the error message, change the font-color to red, etc.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home