JSFormVal 1.0 Documentation

Table of Contents


Introduction

JSFormVal is a JavaScript class which allows web developers to write reliable client-side form validation quickly and easily. It provides methods for validating many different kinds of form data, removing some of the drudgery from this common and unglamorous task.


Demo

Play with the form below to see exactly what JSFormVal does. You can view the source of this page to see the validation code.


(nnn-nnn-nnnn)
(yyyy-mm-dd)
(two-letter US state abbreviation; case-insensitive)
(ZIP+4 okay)


(hh:mm)




Basic Usage

Implementing JSFormVal is a pretty straightforward process. (That's kind of the point.)

These instructions assume that you store your JavaScript files in a subdirectory named "js" in the root of your web directory. If this isn't the case, substitute the correct paths in the <script> tag below.

  1. Download JSFormVal and extract it into your JavaScript directory.

  2. Add the following line to the <head> of your HTML document:

    <script type="text/javascript" src="/js/JSFormVal/JSFormVal.js"></script>
  3. Assign a unique id to each form element that you wish to validate.

  4. Write a JavaScript function named validateForm() (or whatever you like) to be invoked when the form's onsubmit event is fired. Here's an example:

    function validateForm() { var fv = new JSFormVal(); fv.required( "email", "Email Address is a required field." ); fv.email( "email", "That's not a valid email address." ); fv.required( "password1", "Please choose a password." ); fv.required( "password2", "Please retype your password for verification." ); fv.match( "password1", "The two passwords do not match.", "password2" ); return fv.validate(); }
  5. Add an onsubmit attribute to the <form> element. For example:

    <form action="mypage.php" method="post" onsubmit="return validateForm();">

That's it!


Class Methods

Note: All validators, except for required(), consider empty fields to be valid. If you wish to disallow empty values for a particular field, you must use the required() validator, even if you've also assigned other validators to the field.

The first two parameters for all methods (except for validate(), which takes no parameters) are the same. They are:


JSFormVal.date()

void JSFormVal.date( mixed formControl, string errorMsg, string dateFormat )

Ensures that formControl contains a valid date in the specified format.

Valid values for dateFormat are:


JSFormVal.email()

void JSFormVal.email( mixed formControl, string errorMsg )

Ensures that formControl contains a valid email address.


JSFormVal.match()

void JSFormVal.match( mixed formControl, string errorMsg, mixed formControl2 )

Ensures that the values of formControl and formControl2 match.

This is most commonly used for passwords, and sometimes for email addresses.


JSFormVal.numeric()

void JSFormVal.numeric( mixed formControl, string errorMsg[, mixed minValue = null[, mixed maxValue = null[, int minDecimals = 0[, int maxDecimals = 0 ] ] ] ] )

Ensures that formControl contains a numeric value, optionally limited to a specified range and/or number of decimal places.

By default, the validator will accept any integer value (including negative ones), but not decimals. If you wish to modify this behavior, use the optional parameters as described below:


JSFormVal.phone()

void JSFormVal.phone( mixed formControl, string errorMsg )

Ensures that formControl contains a valid telephone number in the format nnn-nnn-nnnn.


JSFormVal.regex()

void JSFormVal.regex( mixed formControl, string errorMsg, mixed regEx[, string regExFlags ] )

Validates formControl against the regular expression defined by regEx.

regEx may be supplied as either a RegExp object, or a string. In the latter case, you can use the optional regExFlags parameter to specify any desired pattern flags.


JSFormVal.required()

void JSFormVal.required( mixed formControl, string errorMsg )

Sets formControl as a required field.


JSFormVal.state()

void JSFormVal.state( mixed formControl, string errorMsg )

Ensures that formControl contains a valid two-letter US state abbreviation. This validator is case-insensitive.


JSFormVal.time()

void JSFormVal.state( mixed formControl, string errorMsg[, bool withSecs = false ] )

Ensures that formControl contains a valid 24-hour time expression (e.g., hh:mm). If the optional parameter withSecs is true, the validator will require a time expression including seconds (e.g., hh:mm:ss).


JSFormVal.url()

void JSFormVal.url( mixed formControl, string errorMsg )

Ensures that formControl contains a valid URL.


JSFormVal.validate()

bool JSFormVal.validate()

Processes all validation. If the form is valid, the method will return true. If not, it will alert the first problem found to the user, give focus to the field in question, and return false.


JSFormVal.zip()

void JSFormVal.required( mixed formControl, string errorMsg[, bool allowPlus4 ] )

Ensures that formControl contains a valid ZIP code.

Set the optional allowPlus4 parameter to true if you wish to allow ZIP+4 codes (e.g., 90210-7283). This parameter defaults to false.


TODO


License

JSFormVal is made available under the Creative Commons Attribution-Share Alike 3.0 License.

In short, this means that you are free to use, distribute, and modify the code, for commercial or non-commercial purposes, provided that all derivative works are made available under the same license, and that the original attribution is left intact. Please read the license for complete details.


Credits & Additional Resources

JSFormVal was written by Travis Miller.

If you've used JSFormVal in a project, I'd like to know about it. If you have bug reports, ideas for improvements or new features, or general technical comments, I'd like to hear about that as well. This is, obviously, an early version—I'd like to develop it into a robust and full-featured tool.

You can get the latest version of JSFormVal from http://abandonedbanana.com/web/js/.