iWeb Toolkit: Javascript Form Field Validator
Javascript Form Field Validator
This handy javascript library provides for simple validation of the data within form fields. It uses a "validator" option within your form input fields, to match against it's library of regular expressions. To use it, follow these simple steps:
-
Download the validation library.
right click the link above, and choose "Save Target As" ... save it as "validator.js"
-
Upload this to your web site, in any directory
-
On the pages where you have a form that needs validation, add this line to your <head> </head> section, changing "/path/to" with the directory where you installed the library:
<script language="javascript" src="/path/to/validator.js"></script>
-
On any form that you want validated, create your form like this:
<FORM id = "theForm" onSubmit = "return validateForm(theForm)">
Note that you must give your form an "id", and also use that "id" in the validateForm clause
-
For any input field that you want validated, add a "validator" clause, ie:
<INPUT TYPE=TEXT name=ip value="128.0.0.1" validator="ipAddress">
Valid "validator" clause values are:
|
Validator Clause
|
Purpose
|
|
zipCode
|
Matches a US zip Code (ie: 12345 or 12345-1234)
|
|
Currency
|
Matches $17.23 or $14,281,545.45
|
|
Time
|
Matches 5:04 or 12:34 but not 75:83
|
|
emailAddress
|
Matches a valid email address (ie: someone@here.com or someone@here.co.uk)
|
|
phoneNumber
|
Matches US phone (###-###-#### or (###) ###-####)
|
|
phoneNumberInternational
|
Matches international phone numbers
|
|
ipAddress
|
Matches a valid IP Address (###.###.###.###, all numbers being <= 255)
|
|
Date
|
Matches a date in xx/xx/xxxx format
|
|
State
|
Matches a US state abbreviation
|
|
SSN
|
Matches a valid US Social Security Number
|
Test It Out: