This is a list of more advanced regular expressions that can be used with the Regular Expression element in Web Form Builder 1.2+.
Don't know what a regular expression is or how to use one? Read this article.
Note: Regular expressions are very complicated! Because of this, we can't help you debug any custom code used in Web Form Builder.
Table of Contents
- Credit Cards
- Domain Names
- HTML Hex Color Codes
- IP Addresses
- MAC Addresses
- Phone Numbers
- Postal Codes
- Roman Numerals
- Social Security Numbers (USA)
- U.S. State & Territory Abbreviations
Credit Cards
/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/
The code above checks that the credit card number entered is a possible value. The codes below are for individual card types.
American Express
/^3[47][0-9]{13}$/
Diner's Club
/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/
Discover
/^6(?:011|5[0-9]{2})[0-9]{12}$/
Mastercard
/^5[1-5][0-9]{14}$/
Visa
/^4[0-9]{12}(?:[0-9]{3})?$/
Domain Names
/^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/
This expression is for domain names, such as "igotmydomain.com" — not URLs, such as "http://www.igotmydomain.com/images/".
HTML Hex Color Codes
/^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$/
This expression accepts HTML color codes written in the 3- or 6-digit hexadecimal format, with or without the #.
IP Addresses
IPv4
/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/
All IPv4 addresses consist of four digits from 0-255 separated by periods.
IPv6
/^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/
Yes, this is amazingly long, but it works! All IPv6 addresses are 8 hexadecimal values separated by colons.
MAC Addresses
/^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/
Each letter pair must be separated by a colon (:).
Phone Numbers
Italy
/^([+]39)?((38[{8,9}|0])|(34[{7-9}|0])|(36[6|8|0])|(33[{3-9}|0])|(32[{8,9}]))([\d]{7})$/
Allows Italian phone numbers with or without the international code +39, but no spaces.
The Netherlands
/^(\d{5}((|-)-\d{4})?)|([A-Za-z]\d[A-Za-z][\s\.\-]?(|-)\d[A-Za-z]\d)|[A-Za-z]{1,2}\d{1,2}[A-Za-z]? \d[A-Za-z]{2}$/
Checks for phone numbers with a leading 0 or the international code +31, and allows spaces and hyphens.
South Africa
/^((?:\+27|27)|0)(=72|82|73|83|74|84)(\d{7})$/
Allows South African telephone numbers with a leading 0 or the international code +27, but no spaces.
Spain
/^[0-9]{2,3}-? ?[0-9]{6,7}$/
Allows Spanish telephone numbers with or without hyphens.
United Arab Emirates
/^(\+97[\s]{0,1}[\-]{0,1}[\s]{0,1}1|0)50[\s]{0,1}[\-]{0,1}[\s]{0,1}[1-9]{1}[0-9]{6}$/
Allows many different formats of UAE phone numbers, including those starting with the international code +97.
United Kingdom
/^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/
Allows a wide variety of UK phone number formats, including the international code +44.
United States
/(([01][\.\- +]\(\d{3}\)[\.\- +]?)|([01][\.\- +]\d{3}[\.\- +])|(\(\d{3}\) ?)|(\d{3}[- \.]))?\d{3}[- \.]\d{4}/
Allows just about any type of US phone number formatting, including area codes in parentheses, leading 1s, and spaces as separators.
Postal Codes
Brazil
/^\d{5}(-\d{3})?$/
Allows 5 digits alone, but also accepts 5 digits with a hyphen and 3 more digits.
Denmark
/^[D-d][K-k]( |-)[1-9]{1}[0-9]{3}$/
Allows valid 6 character Danish postal codes, including optional spaces as separators.
Germany
/\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b/
Allows valid 5 digit German postal codes.
The Netherlands
/^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/
Allows valid 6 character Dutch postal codes including optional spaces as separators.
Spain
/^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$/
Allows valid 5 digit Spanish postal codes.
United Kingdom
/^(([A-Z]{1,2}[0-9]{1,2})|([A-Z]{1,2}[0-9][A-Z]))\s?([0-9][A-Z]{2})$/
Allows all formats of UK postal codes.
United States
/(^\d{5}$)|(^\d{5}-\d{4}$)/
Allows 5 digits alone, but also accepts 5 digits with a hyphen and 4 more digits.
Roman Numerals
/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/
Allows only the characters M, D, L, C, X, V, and I, and in valid roman numeral configurations.
Social Security Numbers (USA)
/^(?!000)([0-6]\d{2}|7([0-6]\d|7[012]))([ -]?)(?!00)\d\d\3(?!0000)\d{4}$/
Accepts both hyphenated and non-hyphenated 9 digit SSNs.
U.S. State & Territory Abbreviations
/^((AL)|(AK)|(AS)|(AZ)|(AR)|(CA)|(CO)|(CT)|(DE)|(DC)|(FM)|(FL)|(GA)|(GU)|(HI)|(ID)|(IL)|(IN)|(IA)|(KS)|(KY)|(LA)|(ME)|(MH)|(MD)|(MA)|(MI)|(MN)|(MS)|(MO)|(MT)|(NE)|(NV)|(NH)|(NJ)|(NM)|(NY)|(NC)|(ND)|(MP)|(OH)|(OK)|(OR)|(PW)|(PA)|(PR)|(RI)|(SC)|(SD)|(TN)|(TX)|(UT)|(VT)|(VI)|(VA)|(WA)|(WV)|(WI)|(WY))$/i
A very long-winded—but effective—way to ensure that a valid state abbreviation has been entered, whether upper or lower case. Includes 9 U.S. territories.