Looking for more validations out of your text fields? Try these Custom Regex validations.
All Major Credit Cards
This regular expression will validate all major credit cards:
American Express (Amex), Discover, Mastercard, and Visa.
1. //All major credit cards regex
2.
'/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|622((12[6-9]|1[3-9][0-9])|([2-8][0-9][0-9])|(9(([0-1][0-9])|(2[0-5]))))[0-9]{10}|64[4-9][0-9]{13}|65[0-9]{14}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})*$/'
Alpha-Numeric Characters
Test for alpha-numeric characters with this regexp.
1. //Alpha-numeric characters only
2. '/^[a-zA-Z0-9]*$/'
Alpha-Numeric Characters With Spaces
Test for alpha-numeric characters and spaces with this regexp.
1. //Alpha-numeric characters with spaces only
2. '/^[a-zA-Z0-9 ]*$/'
Alphabetic Characters
This regex will test for alphabetic characters only (upper and
lowercase).
1. //Alphabetic characters only
2. '/^[a-zA-Z]*$/'
American Express Credit Card
Verify Amex credit cards with this regexp.
1. //Amex credit card regex
2. '/^(3[47][0-9]{13})*$/'
Australian Postal Codes
If you need to verify Australian Postal Codes, use this regular
expression.
1. //Australian Postal Codes
2.
'/^((0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2}))*$/'
Canadian Postal Codes
Tests for valid Canadian Postal Codes.
1. //Canadian Postal Codes
2. '/^([ABCEGHJKLMNPRSTVXY][0-9][A-Z] [0-9][A-Z][0-9])*$/'
Canadian Provinces
Evaluate Canadian province abbreviations with this regular
expression.
1. //Canadian Province Abbreviations
2. '/^(?:AB|BC|MB|N[BLTSU]|ON|PE|QC|SK|YT)*$/'
Date (MM/DD/YYYY)
Validate the calendar date in MM/DD/YYYY format with this regex.
Optional separators are spaces, hyphens, forward slashes, and
periods. The year is limited between 1900 and 2099.
1. //Date (MM/DD/YYYY)
2. '/^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[-
/.](19|20)?[0-9]{2})*$/'
Date (YYYY/MM/DD)
Validate the calendar date in YYYY/MM/DD format with this regex.
Optional separators are spaces, hyphens, forward slashes, and
periods. The year is limited between 1900 and 2099.
1. //Date (YYYY/MM/DD)
2. '#^((19|20)?[0-9]{2}[- /.](0?[1-9]|1[012])[-
/.](0?[1-9]|[12][0-9]|3[01]))*$#'
Digits
This regex will test for digits (whole numbers).
1. //Digits only
2. '/^[0-9]*$/'
Diner's Club Credit Card
Test and verify Diner's Club credit card numbers with this
regexp.
1. //Diner's Club credit card regex
2. '/^(3(?:0[0-5]|[68][0-9])[0-9]{11})*$/'
Emails
This email regex is not fully RFC5322-compliant, but it will
validate most common email address formats correctly.
1. //Email regex
2. '/^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})*$/'
IP Addresses
Test IP Addresses with this regular expression.
1. //IP address regex
2.
'/^((?:(?: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]?))*$/'
Lowercase Alphabetic Characters
This regex will test for lowercase letters.
1. //Lowercase letters only
2. '/^([a-z])*$/'
MasterCard Credit Card
Verify MasterCard credit card numbers with this regex.
1. //MasterCard credit card numbers
2. '/^(5[1-5][0-9]{14})*$/'
Passwords
Test for a strong password with this regex. The password must
contain one lowercase letter, one uppercase letter, one number, and
be at least 6 characters long.
1. //Password regex
2.
'/^(?=^.{6,}$)((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.*$/'
Phone Numbers (North American)
This regex will validate a 10-digit North American telephone
number. Separators are not required, but can include spaces,
hyphens, or periods. Parentheses around the area code are also
optional.
1. //Phone number regex
2. '/^((([0-9]{1})*[- .(]*([0-9]{3})[- .)]*[0-9]{3}[-
.]*[0-9]{4})+)*$/'
Social Security Numbers
If you need to validate US Social Security Numbers, use this
regular expression
1. //SSN regex
2. '/^([0-9]{3}[-]*[0-9]{2}[-]*[0-9]{4})*$/'
UK Postal Codes
This regexp verifies UK Postal Codes.
1. //UK Postal Codes regex
2. '/^([A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2})*$/'
Uppercase Alphabetic Characters
This regex will test for uppercase letters.
1. //Uppercase letters only
2. '/^([A-Z])*$/'
URLs
This URL regex will validate most common URL formats correctly.
1. //URL regex
2.
'/^(((http|https|ftp):\/\/)?([[a-zA-Z0-9]\-\.])+(\.)([[a-zA-Z0-9]]){2,4}([[a-zA-Z0-9]\/+=%&_\.~?\-]*))*$/'
US States
Validate all 2-letter US State abbreviates with this regular
expression.
1. //US States regex
2.
'/^(?:A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|PA|RI|S[CD]|T[NX]|UT|V[AT]|W[AIVY])*$/'
US ZIP Codes
This regexp verifies US ZIP Codes, with an optional 4 number ZIP
code extension.
1. //US ZIP Codes regex
2. '/^([0-9]{5}(?:-[0-9]{4})?)*$/'
Visa Credit Card
Verify Visa credit card numbers with this regex.
1. //Visa credit card numbers
2. '/^(4[0-9]{12}(?:[0-9]{3})?)*$/'
Here are some Regex Tutorials:
While this page doesn't go in depth on how to learn regular
expressions, I will point you to some tutorials so that if you need
to modify any of the above regexes or create your own, you'll be
able to do so.
Regex Resources And Reference Sheets
There are a number of different regex cheat sheets, libraries,
testers, and other resources around the web. Here are just a few of
them.