(Last update: 29-Apr-2005)
The easy, three step way to use this script's validation:
Step 1: Change the form's onSubmit code to "return standardValidation()"
Step 2: For all single-value keyword fields on the form, make sure the first option is a dummy value like "" or "(Select One)"
Step 3: In the form's JS Header, create a JavaScript function named registerValidationElements() that calls this library's
registerValidationElement function for each object to be validated. (Your registerValidationElements function will be called
only the first time that standardValidation() is called.)
The flexible way to use this script's validation on your forms:
For all single-value keyword fields on the form, make sure the first option is a dummy value like "" or "(Select One)"
In your onSubmit function,
1. Call clearValidationMsgs() to clear any validation messages that might have existed the previous time you called onSubmit
2. Use validateElement function for each field you'd like to validate. This returns true if valid. (The arguments for
validateElement are documented below.)
3. if any of the validateElement calls returned false, alert(validationMsg()) and return false, otherwise return true.
The flexible way allows you to write validation that are dependent on certain criteria, such as not validating a
Price field if a Quantity field is blank. It also allows you to use your own validation message code... you don't have to
call clearValidationMsgs() or validationMsg() at all if you'd rather write your own UI.
The registerValidationElement and validateElement functions use up to six arguments:
registerValidationElement(o, sName, bRequired[, vMax[, vMin[, sType]]])
validateElement(o[, sName[, bRequired[, vMax[, vMin[, sType]]]]])
| Arg | Type | Comments |
| o | Object | field to be validated. e.g. document.forms[0].ZipCode |
| sName | String | title to use when prompting any invalid values to the user. e.g. "Zip Code", or "Name" |
| bRequired | true/false | boolean value indicating whether or not this field is required. e.g. true, or false |
| vMax | Number | If sType is "number", "date" or "datetime", maximum value allowed.
If sType is "text" then maximum length allowed.
If sType is "date" or "datetime" then a date value or date string.
Time parts of date values are ignored.
Time parts of date strings are not allowed.
Date values and date strings are inclusive. (1/1/2000 11:00 PM is considered within a max of "1/1/2000")
e.g. 10 or "10/5/2090" or a date object. |
| vMin | Number | If sType is "number", "date", or "datetime", minimum value allowed.
If sType is "text" then minimum length allowed.
If sType is "date" or "datetime" then a date value or date string.
Time parts of date values are ignored.
Time parts of date strings are not allowed.
Date values and date strings are inclusive.
e.g. 5 or "5/10/1990" or a date object |
| sType | String | This argument allows you to override the default type of the object.
If null or not supplied, the type will be o.type. This is great for most fields, but
in the case of an INPUT field, o.type returns "text". In some cases, the input in question
holds date or numerical values. You can augment the validation behavior by using "number",
"date", "datetime", "percent", "integer", or "email" for this argument.
If "datetime", dates without time values are still valid.
Note: We've seen issues with fields that have been solved by explicitly passing the type
in this argument. If you're validating checkbox or radio objects, you
shouldn't-but-might have to include "checkbox" or "radio" here.
e.g. "number", "date", "datetime", "percent", "integer", "email", "checkbox", "radio", "textarea",
"select-one", or "select-multiple" |
In the JS Header:
function registerValidationElements() {
var F = document.forms[0];
registerValidationElement(F.LastName, "Last Name", true) // input text that can't be empty
registerValidationElement(F.Age, "Age", false, 150, 1, "integer") // non-required input that must be a valid integer between 1 and 150 inclusive
registerValidationElement(F.State, "State", true) // keyword field of States that must have a value
registerValidationElement(F.Interests, "Interests", true) // checkboxes where at least one must be checked
registerValidaitonElement(F.Phone1, "Home Phone", false, null, 10) // input field that, if filled, must have at least ten characters
registerValidationElement(F.Discount, "% Discount", false, 100, 0, "percent") // non-required percent value between 0 and 100 inclusive.
registerValidationElement(F.BirthDate, "Birth Date", false, new Date(), "1/1/1900", "date") // optional date between 1900 and today
registerValidationElement(F.Email, "Email Address", true, null, null, "email") //required email address field.
}
In the onSubmit:
return standardValidation()