Form Validation - Usage

form_validation.js defines the following functions:

getFormErrors()

Synopsis

getFormErrors(formReference)

Arguments

formReference

A reference to the form to check.

Returns

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).

Description

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:

  • Various credit cards ('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()

Synopsis

isAlphabetic(string, ignoreWhiteSpace)

Arguments

string

The string to be tested.

ignoreWhiteSpace

An 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.

Returns

true if string consists of only alphabetical characters, false otherwise.

Description

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()

Synopsis

isAlphanumeric(string, ignoreWhiteSpace)

Arguments

string

The string to be tested.

ignoreWhiteSpace

An 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.

Returns

true if string consists of only alphanumeric characters, false otherwise.

Description

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()

Synopsis

isNumeric(string, ignoreWhiteSpace)

Arguments

string

The string to be tested.

ignoreWhiteSpace

An 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.

Returns

true if string consists of only numeric characters, false otherwise.

Description

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()

Synopsis

isValidCreditCard(number, type)

Arguments

number

The number to be tested.

type

The optional card type. The following are valid values: 'visa', 'mastercard', 'american express', 'diners card', 'discover', 'enroute', 'jcb'.

Returns

true if number corresponds to a valid credit card number under the LUHN formula (mod10 test), false otherwise.

Description

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()

Synopsis

isValidEmail(address)

Arguments

address

The address to be tested.

Returns

true if address has the appearance of an email address, false otherwise.

Description

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()

Synopsis

isValidEmailStrict(address)

Arguments

address

The address to be tested.

Returns

true if address has the appearance of an email address, false otherwise.

Description

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()

Synopsis

isValidLength(string, min, max)

Arguments

string

The string to be tested.

min

The minimum allowed length.

string

The maximum allowed length.

Returns

true if the number characters in string is between min and max, false otherwise.

Description

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()

Synopsis

isValidPostalcode(postalcode)

Arguments

postalcode

The string to be tested.

Returns

true if postalcode is a valid postal code, false otherwise.

Description

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()

Synopsis

isValidUSPhoneNumber(phoneNumber) or
isValidUSPhoneNumber(areaCode, prefixNumber, suffixNumber)

Arguments

phoneNumber

The phone number string to be tested.

areaCode

The area code string of the phone number to be tested. In the phone number (123)456-7890, the area code is 123.

prefixNumber

The prefix string of the phone number to be tested. In the phone number (123)456-7890, the prefix is 456.

suffixNumber

The suffix string of the phone number to be tested. In the phone number (123)456-7890, the suffix is 7890.

Returns

true if the number represented by the arguments is a valid phone number in the US or Canada, false otherwise.

Description

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()

Synopsis

isValidZipcode(zipcode)

Arguments

zipcode

The string to be tested.

Returns

true if zipcode is a valid zip code, false otherwise.

Description

isValidZipcode() tests zipcode to determine if it looks like a valid zip code. Valid postal codes consist of 5 numbers. Whitespace is ignored.

removeBadCharacters()

Synopsis

removeBadCharacters(string)

Arguments

string

The string to be cleansed.

Returns

A cleansed version of string.

Description

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()

Synopsis

removeSpaces(string)

Arguments

string

The string to be cleansed.

Returns

A version of string without spaces.

Description

removeSpaces() removes all whitespace characters from a string.

trimWhitespace()

Synopsis

trimWhitespace(string)

Arguments

string

The string to be trimmed.

Returns

A version of string without leading or trailing whitespace.

Description

removeSpaces() removes whitespace characters from the beginning and end of a string. Whitespace characters between non-whitespace characters in the string are retained.

More about this script

Related to this script

Licensing

This script is released under a Creative Commons License.