form_validation.js defines the following functions:
getFormErrors()getFormErrors(formReference)
formReferenceA reference to the form to check.
An array containing the error strings that correspond to the errors in the form data (these are ones that you have predefined - see the Description section below).
getFormErrors() runs a number of tests on the elements in a form and reports any errors encountered. You specify the tests to perform on each element and the error message that should be returned if the element fails the test. The tests available include:
required (for text, textarea, password, file upload, select, multiple select, and radio button elements)The user must enter a value (text, textarea, password, file upload) or make a selection (select, multiple select, radio button) for the element. To specify that this test be used on element, set element.required = true and supply an error message by assigning one to element.requiredError.
maxlength (for text, textarea, and password elements)The value entered has a maximum character length. To specify that this test be used on element, assign the maximum length to element.maxlength and supply an error message by assigning one to element.maxlengthError.
minlength (for text, textarea, and password elements)The value entered has a maximum character length. To specify that this test be used on element, assign the minimum length to element.minlengthand supply an error message by assigning one to element.minlengthError.
pattern (for text and textarea elements)The value entered must match a specific pattern. Valid patterns include:
'visa', 'mastercard', 'american express', 'diners club', 'discover', 'enroute', or 'jcb')'email''zipcode''postal code''zip and postal code''alphabetic''numeric''alphanumeric'To specify that a pattern test be used on element, assign the required pattern string to element.pattern and supply an error message by assigning one to element.patternError
disallowEmptyValue (for select)The user must select only selections with a value other than an empty string ("") should be selected. Titles or directions for <select> are frequently stored as the text for some <option> elements within the list. An example would be a dropdown containing a list of years: the first option could be "Pick a year" with the second option being a divider between this title and the actual year options ("----------"). Both of these options should not be selectable by the user. To prevent the user from selecting them, set the value for both options to be the empty string (""), set element.disallowEmptyValue = true, and supply an error message by assigning one to element.disallowEmptyValueError.
See the source of the example page for an example of this function's usage.
isAlphabetic()isAlphabetic(string, ignoreWhiteSpace)
stringThe string to be tested.
ignoreWhiteSpaceAn optional boolean that specifies whether to ignore spaces and tabs in string. If false, any spaces or tabs will be considered non-alphabetical characters and strings containing these characters will fail the test. If true, spaces and tabs will be disregarded. Defaults to false.
true if string consists of only alphabetical characters, false otherwise.
isAlphabetic() tests it's first argument to determine if it consists of only the characters a through z, A through Z. White space in string can be ignored by including a value of true for the optional second argument.
isAlphanumeric()isAlphanumeric(string, ignoreWhiteSpace)
stringThe string to be tested.
ignoreWhiteSpaceAn optional boolean that specifies whether to ignore spaces and tabs in string. If false, any spaces or tabs will be considered non-alphanumeric characters and strings containing these characters will fail the test. If true, spaces and tabs will be disregarded. Defaults to false.
true if string consists of only alphanumeric characters, false otherwise.
isAlphanumeric() tests it's first argument to determine if it consists of only the characters a through z, A through Z, 0 through 9. White space in string can be ignored by including a value of true for the optional second argument.
isNumeric()isNumeric(string, ignoreWhiteSpace)
stringThe string to be tested.
ignoreWhiteSpaceAn optional boolean that specifies whether to ignore spaces and tabs in string. If false, any spaces or tabs will be considered non-numeric characters and strings containing these characters will fail the test. If true, spaces and tabs will be disregarded. Defaults to false.
true if string consists of only numeric characters, false otherwise.
isNumeric() tests it's first argument to determine if it consists of only the characters 0 through 9. White space in string can be ignored by including a value of true for the optional second argument.
isValidCreditCard()isValidCreditCard(number, type)
numberThe number to be tested.
typeThe optional card type. The following are valid values: 'visa', 'mastercard', 'american express', 'diners card', 'discover', 'enroute', 'jcb'.
true if number corresponds to a valid credit card number under the LUHN formula (mod10 test), false otherwise.
isValidCreditCard() tests number using the LUHN formula (mod10 test) to determine if it would be a valid credit card number. Numbers must be between 13 and 16 digits long. If a type is included, then further checks are made to make sure the number corresponds to the specific requirements for the type of card. It doesn't check if the credit card number has been assigned or if the associated card is in good standing.
isValidEmail()isValidEmail(address)
addressThe address to be tested.
true if address has the appearance of an email address, false otherwise.
isValidEmail() tests address to determine if it looks like it might be a valid email address. The rules governing email addresses are very loose - postmaster@localhost is still a valid email address form. If the @ appears at or after the 3rd character and the name and domain portions contain no illegal characters, then the address is considered valid.
isValidEmailStrict()isValidEmailStrict(address)
addressThe address to be tested.
true if address has the appearance of an email address, false otherwise.
isValidEmailStrict() tests address to determine if it looks like it might be a valid email address. It is stricter that isValidEmail() in that it requires the part of the address after the "@" be two strings separated by a ".". In such a case, postmaster@localhost is not a valid email address but someone@example.com is. Valid addresses must still pass the isValidEmail() test.
isValidLength()isValidLength(string, min, max)
stringThe string to be tested.
minThe minimum allowed length.
stringThe maximum allowed length.
true if the number characters in string is between min and max, false otherwise.
isValidLength() tests string to determine if it's length falls between minimum and maximum limits. min can be set to 0 if there is no lower limit. max can be set to Number.MAX_VALUE if there is no upper limit.
isValidPostalcode()isValidPostalcode(postalcode)
postalcodeThe string to be tested.
true if postalcode is a valid postal code, false otherwise.
isValidPostalcode() tests postalcode to determine if it looks like a valid postal code. Valid postal codes consist of 6 alternating letters and numbers and start with a letter. Between the 3rd and 4th character, a hyphen is allowed. Whitespace is ignored.
isValidUSPhoneNumber()isValidUSPhoneNumber(phoneNumber) or
isValidUSPhoneNumber(areaCode, prefixNumber, suffixNumber)
phoneNumberThe phone number string to be tested.
areaCodeThe area code string of the phone number to be tested. In the phone number (123)456-7890, the area code is 123.
prefixNumberThe prefix string of the phone number to be tested. In the phone number (123)456-7890, the prefix is 456.
suffixNumberThe suffix string of the phone number to be tested. In the phone number (123)456-7890, the suffix is 7890.
true if the number represented by the arguments is a valid phone number in the US or Canada, false otherwise.
isValidUSPhoneNumber() tests a phone number string to determine if it looks like a valid phone number in the US or Canada. A valid phone number, the area code is 3 digits, the prefix part is 3 digits and the suffix part is 4 digits.
When given a single argument, non-number characters are removed ((123) 456-7890 becomes 1234567890) and the result is split into area code, prefix and suffix parts by assuming the last 4 digits are the suffix, the previous 3 are the prefix and what remains is the area code. This allows numbers without area codes to be passed and validated.
In the three argument version, the first argument must be the areacode, the second is the prefix part, and the third is the suffix part. In order to validate a phone number without an area code with this version of the function, use '999' as the first argument.
isValidZipcode()isValidZipcode(zipcode)
zipcodeThe string to be tested.
true if zipcode is a valid zip code, false otherwise.
isValidZipcode() tests zipcode to determine if it looks like a valid zip code. Valid postal codes consist of 5 numbers. Whitespace is ignored.
removeBadCharacters()removeBadCharacters(string)
stringThe string to be cleansed.
A cleansed version of string.
removeBadCharacters() removes all illegal keyboard characters from a string. For form input, the returned, cleansed version of the string can be written back into the form element before submission of the form.
removeSpaces()removeSpaces(string)
stringThe string to be cleansed.
A version of string without spaces.
removeSpaces() removes all whitespace characters from a string.
trimWhitespace()trimWhitespace(string)
stringThe string to be trimmed.
A version of string without leading or trailing whitespace.
removeSpaces() removes whitespace characters from the beginning and end of a string. Whitespace characters between non-whitespace characters in the string are retained.
This script is released under a Creative Commons License.