Example Forms

The following examples demonstrate how to apply the Jel.FormValidator to three XHTML forms in different ways. Each example comprises a sample form to interact with, followed by the JavaScript code used to apply the validator. For reference, the validation classes attached to each field are displayed in the title for each (mouse over each field to view).

If you haven't yet done so, it might be good to read Jel.FormValidator and XHTML to understand how to setup your forms to ensure the validator works correctly; most of the required setup steps are standard best-practices for XHTML forms, but there are some workarounds that JEL uses in special cases.

For each example, try hitting "submit" before filling anything in, and then observe the behaviour as you correct errors, try to submit again, and so on.


Example 1: Default Behaviours

A validator is created on the following form with no customisation at all, using a simple one-line constructor call. The default way to present the results of form validation is to show an alert box.

Gender: *

The Code

Features to observe


Example 2: Results container, inline error messages, and field comparisons.

This example form introduces a container element for validation results, inline error messages, and an equality comparison between Password and Confirm Password. Note that inline error messages are actually active by default, but will only show up if there are DOM elements setup to contain them, which is the case here. The default way to do this is to provide each field with an element with the same ID as the field but suffixed with "-inline" (the suffix can be changed if desired, in the constructor options hash).

Gender: *

The Code


Example 3: Extending Jel.FormValidator

This advanced example illustrates how you can extend the FormValidator class itself to suit requirements specific to your site. Try to submit the form below with some required fields missing and notice the way that the container <div> around both the label and field is highlighted. This functionality is not directly supported by Jel.FormValidator; only fields and attached labels are marked as culprits automatically. This makes sense, given that everyone (hopefully) builds forms by marking up the fields inside, and attaching <label> elements to each via the for attribute, but not everyone will markup the container elements in the same way.

Inspired by techniques discussed in Justin Palmer's excellent article Avoiding Bloat in Widgets, Jel.FormValidator was structured to allow developers to extend its functionality through subclassing; classes in Jel are able to be subclassed because they extend the Base object in Dean Edwards Base library. See the code below for specific details on how this is achieved.

Gender: *

The Code

In this particular example, we create a Site.FormValidator by extending Jel.FormValidator, and overriding the methods displayCulprit, releaseCulprit, and constructor, as shown below. Note that each call to this.base is the mechanism for calling the super-class method, made possible by Dean Edwards' Base.

Once this class is setup, we simply apply our new specialised class to the example form:

All Site Content © 2007 Travis Hensgen.  Icons by IconBuffet and FamFamFam.