Here is a handy collection of regular expressions, for use with PHP preg_match. If you need to check for properly formatted US states (two letters), US ZIP codes, US phone numbers, US Social Security Numbers, URLs for web sites, and properly formatted email addresses, read on:
Email addresses
Matches major top level domains plus two-letter country codes. No it doesn’t check for the validity of two letter country codes. Yes, you could add them to the list of top level domains at the end of the string. And that would be a really long regular expression! It does allow for subdomains (joe@test.blow.com).
TIP: When copied, make sure it appears on one line. Your best bet is to make sure your server’s regex matching is set to case-insensitivity, otherwise addresses like joe@blow.cOm won’t be matched:
/^[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]{2}|aero|biz|com|coop|edu|gov|info|jobs|mil|mobi|museum|name|net|org|travel)$/i
States (two letters)
Matches the two letter United States Postal Service abbreviations for all fifty states:
/^(A[LKSZRAP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADL N]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD] |T[NX]|UT|V[AIT]|W[AIVY])$/
ZIP Codes
Matches ZIP or ZIP+4 as used in the United States. Doesn’t properly handle 00000 or 0000-####, which are non-existent ZIP codes.
/^[0-9]{5,5}([- ]?[0-9]{4,4})?$/
Phone Numbers
The following regex attempts to match numbers that adhere to the North American Numbering Plan. It matches common entry combinations, such as #######, ###.###.####, ###-###-####, ### ### ####, and (###) ### ####.
'\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}'
A quick tip: If you want to then replace whatever the customer enters and store the number in a specific format, use preg_replace. For example, here’s how to replaces all the above formats with (###) ###-####:
preg_replace('\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}', $original_input);
Social Security Number
Matches various permutations of a US Social Security Number, such as ###-##-####, #########, ### ## ####:
/^[0-9]{3}[- ]?[0-9]{2}[- ]?[0-9]{4}$/
URLs
Holy cow, this is a hotly debated genre of regex. Well, I’m interested only in matching strings such as http://www.domain.com, http://domain.com, https://test.domain.com, https://localhost:8080, and ftps://domain.com. The answer is forthcoming. I hope.
/\b((ht|f)tps?:\/\/(([0-9a-z-]+\.)+([a-z]{2}|aero|biz|com|coop|edu|gov|info|jobs|mil|mobi|museum|name|net|org|travel))(:[0-9]{1,5})?(\/[^ ]*)?)\b/i
NOTE: This URL regex sucks, because it allows domains and subdomains to begin with a dash, which is illegal. Don’t use it.
Pingback: Custom Attribute Type Question Type Concrete5