Categories
PHP

Phone Number and Zip Code Validation Regex

In this tutorial, you’ll see how regular expressions work, we use some of the data validation tasks to demonstrate how we can use regular expressions to validate usernames, phone numbers, and postal codes.

  1. Validating usernames
  2. Validating US phone numbers
  3. Validating US and Canadian postal codes

Validating Usernames

  1. Username must consist of alphanumeric characters, dashes, and underscore.
  2. Username length must be between 8 and 50 characters.

The regular expression for this turns out to be pretty simple: [[:alnum:]_-]{8,50}. We can then use the preg_match() function to actually make sure that a username conforms to this pattern, as follows:

<?php
 $username = 'BrainBell_com';
 $pattern = '/^[[:alnum:]_-]{8,50}$/';
 $match = preg_match($pattern, $username);
 if ($match === 1)
  echo 'Valid username';
 else if ($match === 0)
  echo 'Invalid username';
 else
  echo preg_last_error_msg();

Matching Phone Numbers

A slightly more interesting example would be to match U.S. and Canadian telephone numbers. In their most basic forms, these are a sequence of seven digits, usually separated by some character such as a space, a dash -, or a dot .. A regular expression for this would be as:

[0-9]{3,3}[-. ]?[0-9]{4,4}.

This simple expression says match exactly three digits ([0-9]{3,3}), followed optionally by a single dash, period, or space ([-. ]?), and then match exactly four more digits ([0-9]{4,4}).

The area code, which is a three-digit number, can optionally be wrapped in parentheses, or not wrapped in parentheses but separated from the other digits by a space, dash, or dot. Our regular expression begins to get more complicated. The new portion of the expression to match the area code will look like this:

\(?[0-9]{3,3}\)?[-. ]?

Because the ( and ) characters are meta-characters used by regular expressions, we have to escape them with the backslash \ to use them as characters we want to match. Our complete regular expression thus far would be this:

\(?[0-9]{3,3}\)?[-. ]?[0-9]{3,3}[-. ]?[0-9]{4,4}

Example: Validating phone numbers

<?php
 $pattern = '/^\(?[0-9]{3,3}\)?[-. ]?[0-9]{3,3}[-. ]?[0-9]{4,4}$/';
 echo preg_match($pattern, '123 456 7890'); # 1
 echo preg_match($pattern, '123-456-7890'); # 1
 echo preg_match($pattern, '123.456.7890'); # 1
 echo preg_match($pattern, '(123) 456 7890');# 1

Matching Postal Codes

<?php
 $pattern = '/[0-9]{5,5}([- ]?[0-9]{4,4})?/';

U.S. postal codes (Zip codes) are a sequence of five digits followed optionally by the “plus 4,” which is a dash character followed by four more digits. A regular expression for this is as follows:

<?php
 $pattern = '/^[0-9]{5,5}([- ]?[0-9]{4,4})?$/';
 echo preg_match($pattern, '63625'); # 1
 echo preg_match($pattern, '63625 7241'); # 1
 echo preg_match($pattern, '63625-7241'); # 1

Validating Canadian Postal Codes

Canadian postal codes are always of the format X#X #X#, where # represents a digit and X a letter from the English alphabet. A regular expression for this would be as follows:

[A-Za-z][0-9][A-Za-z] [0-9][A-Za-z][0-9]

Example: Validating Canadian Postal Codes:

<?php
 $pattern = '/^[A-Za-z][0-9][A-Za-z]\s[0-9][A-Za-z][0-9]$/';
 echo preg_match($pattern, 'A0A 0A0'); # 1

Note: A Canadian postal code always starts with the letters: ABCEGHJKLMNPRSTVXY. We could rewrite our regular expression as follows:

<?php
 $pattern = '/^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]';
 $pattern .= '[0-9][A-Za-z]\s[0-9][A-Za-z][0-9]$/';
 echo preg_match($pattern, 'A0A 0A0'); # 1

More Regular Expressions Tutorials: