OpenNTF.org - Smart Form Validation
My Links (Not logged in)
Code Bin Search
 
Hosted by Prominic.NET
Rate This Code
5 - brilliant stuff
4 - very nice
3 - average
2 - needs work
1 - bad
   OpenNTF Code Bin
About This Code
Brief Description:
Smart Form Validation 
Rating:
Rating: 5 , Number of votes: 2 
Contributor:
John Smart 
Category:
Javascript 
Type:
Miscellaneous 
Document Release:
2.32 
Notes Version:
R5.x, R6.x 
Last Modified:
29 Apr 2005 
OpenNTF Disclaimer

All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.

Code / Description
(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]]]]])
ArgTypeComments
oObjectfield to be validated. e.g. document.forms[0].ZipCode
sNameStringtitle to use when prompting any invalid values to the user. e.g. "Zip Code", or "Name"
bRequiredtrue/falseboolean value indicating whether or not this field is required. e.g. true, or false
vMaxNumberIf 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.
vMinNumberIf 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
sTypeStringThis 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"

Usage / Example
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()
Code Attachments
SmartValidation.js (33 Kbytes)
 Comments

No documents found

 Add your comment!