Introduction
This is an Object Oriented Javascript form validation framework. I created this because I have written countless forms which require client side validation, each time I did a form I found myself copy and pasting a simple Javascript into the page to do basic form validation.
I designed this component to be very easy to use, easy to add to existing forms and require very little code from the programmer. Whilst creating this I also had the W3C web standards in mind, so you will find that it will not cause your page to fail a W3C validation test.
Below I will explain everything about the Validator, but be sure to download the samples attached which you can play around with.
Simple Usage
To use the basic functionality all you have to do is include the classes in the page you want to use it on. All the classes are inside the easy_validation.js file.
javascript Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Do you accept our terms? <input type="checkbox" id="terms" />
<input type="submit" name="submit_button" />
</form>
</body>
</html>
The example above will produce a popup with the list of validation errors (if any). The only special things about the form are that each element has ID which is needed for the Validator to pickup the field, and the onsubmit event of the form element. That event tells the form to call our validator function when submitted, if it fails the validation tests, the form will not be submitted.
We create a function called validate in the head section of the page, which is where we set the parameters for the Validator object.
The addField method is self explanatory, however I will explain each of the arguments:
Advanced Usage
I call this section Advanced Usage, but it is still very easy to use, the functionality that we make use of in this section is just more advanced.
You can show the error messages on the actual web page. I recommend doing this because it looks a lot more professional than just a popup.
To show the errors on your page, you just need to tell the Validator object to enable errors on the page, and also tell it which Div on your page to place the error HTML in.
javascript Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Do you accept our terms? <input type="checkbox" id="terms" />
<input type="submit" name="submit_button" />
</form>
</body>
</html>
Notice in the above example we call setOutputType() and pass in “DIV”. There are three possible values for this argument which are ALERT – show errors in a popup, DIV – show errors in a div on the page and “BOTH” to show the errors in an alert and a div.
Also notice that when using a Div to display the errors, you must tell the Validator which div to use, by passing in the ID.
Last edited by the182guy; Nov 15th, 2008 at 12:20 PM.
More Advanced Features
In the above section I explained about displaying the errors on the page, which is great but what if you want to customise the way the errors are displayed on the page? You can create your own error template and have the errors displayed exactly how you want. Here’s an example
javascript Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Do you accept our terms? <input type="checkbox" id="terms" />
<input type="submit" name="submit_button" />
</form>
</body>
</html>
In the above code, I create a new ErrorTemplate object and set its properties, then assign it to the validation object to make use of.
The properties we set are:
Header
The header can be any HTML you want to appear right above the errors. If you want to put the errors in a HTML list then the header is where you would put the open list tag like <ul> or <ol>. The header section of the template is also useful for a title such as Error!
Item
The item section is what will be repeated for each validation error that occurs. In the item section you place a special reference in the code which will be replaced with the actual validation error. The reference which will be replaced is {ERROR_MESSAGE}
Footer
The footer section is useful for closing a list if you have opened one in the head section, e.g. </ul> or </ol>.
Last edited by the182guy; Nov 15th, 2008 at 12:20 PM.
Even More Advanced Features
You might be thinking that, what if I have a complicated validation to perform? You can write your own custom validation function and just pass it to the Validator, the Validator will then handle it all for you. Just make sure your custom function returns true or false. Example:
javascript Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Enter your Name: <input type="text" id="my_name" value="Chris" /><br />
Enter a password: <input type="password" id="password1" value="Chris" /><br />
Confirm the password: <input type="password" id="password2" value="Chris" /><br />
<input type="submit" name="submit_button" />
</form>
</body>
</html>
In the above example I demonstrate creating a custom validation function and having the Validator handle it for me. The custom validation is to check if two password textboxes match.
For those of you that might argue that a framework like this is going to be too heavy to use – i.e. large download, then you can put the code through an optimiser which will remove all white space and unnecessary characters, this will reduce the file size by a lot.