Foul improves validation and your outlook

Opinion
Jun 13, 20052 mins

* Foul JavaScript form validation language

When you are developing content it is amazing how often you find yourself reinventing the wheel … over and over and over again.

One place where this happens with monotonous regularity is in the building of forms. While validating content server-side isn’t such a huge task creating a decent validity checker for client-side input seems to be one of those endlessly repeating task because you usually do it specifically for each form.

Here’s a solution: Foul, a JavaScript based, form validation language (see editorial links below) from Blue Line City. As the author, Bryan English, notes:  “If you hate all the rigamarole you have to go through just to check a simple Web form, then this script is what you need … I’m a veteran when it comes to form validation. I know first hand how much of a pain it is. This script makes form validation stupidly easy and still allows for that tweaking that programmers are always looking for.”

Foul is, indeed, simple. You simply upload foul.js to your Web site, include the code in your Web page containing the form you want to validate, attach Foul to the form using the onSubmit event handler and create validation tests. This is what the validation tests look like:

foul.when(‘~email~ is null’,’Email is a required field.’);

foul.when(‘~email~ is not email’,’Please enter a valid email address’);

As you can see Foul can test for empty fields and fields that don’t meet specific criteria (note that in the example above “email” is a pre-defined data template that checks for e-mail-like characters). You can also create compound statements using ‘and’, ‘or’ and parentheses.

Submitting a validated form that contains errors trapped by Foul will raise a dialog box alerting the user to each problem. Fields can also be checked when the focus is moved from one field to another.

Foul isn’t bulletproof (you can avoid input validation if you know what you’re doing) so server-side validation can’t be disposed of. On the other hand being able to detect invalid form field content before submission should improve the usability of your site immensely and not having to recreate form validation code for each form should also improve your outlook.