Custom Validations for Web Form Fields
OneTrust web forms support custom validations that can limit the acceptance criteria for input fields. This is accomplished by adding JavaScript Regular Expressions (RegEx).
The following custom validation examples can be used to support specific acceptance criteria for your web form input fields.
First Name/Last Name
^([A-Za-z-.',\s]{1,30})$
Accepts upper and lower case letters, hyphens, periods, commas, single quotation marks, and spaces for up to 30 characters. Rejects special characters and numbers.
Example:
-
Accepts: John Doe | O'Doyle | Smith Jr.
-
Rejects: Jon $mith | 5teven
^[A-Za-z0-9_\s-]*$
Accepts hyphens and spaces. Rejects commas.
Example:
-
Accepts: John- Doe
-
Rejects: John, Doe
Phone Number
^\d{3}-?\d{3}-?\d{4}$
Accepts phone numbers in the form of XXX-XXX-XXXX and XXXXXXXXXX.
Example:
-
Accepts: 123-456-7890 | 1234567890
Date of Birth
^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$
Accepts date input in form of MM/DD/YYYY with a year range of 1900-2099.
Example:
-
Accepts: 12/31/1999 | 01/09/2099
-
Rejects: 13/01/1999 | 2/3/2000
Address
^(?!.*\b(P\.?O\.?\s?Box|PO\s?Box)\b).*[\d]+[\s\w\.#-]+$
Rejects any variation of PO Box.
Example:
-
Rejects: PO Box | P.O Box | P.O. Box
Zip Code
^((?!90000)(([9-9][0-6][0-1][0-6][0-2])|([9-9][0-5][0-9][0-9][0-9])|([9-9][0-6][0-0][0-9][0-9])|([9-9][0-6][0-1][0-5][0-9])))$
Accepts any string of 5 digits. Use custom validation to restrict the range of numbers for a zip code.
Example:
California zip codes range from 90001-96162. The custom validation only accepts California zip codes.
-
Accepts: 90001 | 96162 | 95999
-
Rejects: 90000 | 96169
Social Security Number
^(?!000)[0-9]{3}([ -]?)(?!00)[0-9]{2}\1(?!0000)[0-9]{4}$
Accepts SSN in the form of XXX-XX-XXXX and XXXXXXXXX. Rejects “0”s as inputs.
Example:
-
Accepts: 123-40-6789 | 123456789
-
Rejects: 123-00-1234 | 123001234