• JavaScript, PHP, and Regular Expressions for International and U.S. Phone Number Formats

    Date: 2014.03.13 | Category: Programming & Functions | Tags: ,,,,,,,,,

    I have been looking all over the web for a regular expression to use in JavaScript functions to help with formatting phone number links and printing phone numbers on pages for iPhones, Android, and other mobile devices. This process would naturally affect traditional internet browsers for traditional computers but I went down this path mostly for tracking phone number touch events in Google Analytics on only mobile devices. I finally had the epiphany today that created one expression that covered most international and United States phone number formats.

    International and U.S. Phone Number Regular Expression

    The red areas, represent the numbered portions. Areas in gray represent the options for spaces, dashes, periods, or sometimes nothing between numbers (e.g. the question marks).
    /\d?(\s?|-?|\+?|\.?)((\(\d{1,4}\))|(\d{1,3})|\s?)(\s?|-?|\.?)((\(\d{1,3}\))|(\d{1,3})|\s?)(\s?|-?|\.?)((\(\d{1,3}\))|(\d{1,3})|\s?)(\s?|-?|\.?)\d{3}(-|\.|\s)\d{4}/

    Example in Using this Expression in JavaScript:

    var patterns = new RegExp(/\d?(\s?|-?|\+?|\.?)((\(\d{1,4}\))|(\d{1,3})|\s?)(\s?|-?|\.?)((\(\d{1,3}\))|(\d{1,3})|\s?)(\s?|-?|\.?)((\(\d{1,3}\))|(\d{1,3})|\s?)(\s?|-?|\.?)\d{3}(-|\.|\s)\d{4}/);
    inner_code = “This is a typical United States number format: 1 (555) 555-1212”;
    number_found = patterns.exec(inner_code);  //This use of exec() will find the first occurrence only. To find several phone numbers, add a g to the end of the expression (e.g. /your_expression/g). It’ll return an array. Then you can use number_found[0] and increment the number between the brackets to return each phone number found.
    alert(number_found);

    In Javascript or PHP (and likely other programming languages), you can define a variable with the expression. Just don’t use quotes around the expression. You can also put the expression between the parenthesis in something like var patterns = new RegExp() as I used above, with or without quotes. At that point you can use the variable an any function that responds to pattern matching. I list examples at the bottom of this article.

    Example Phone Number Formats

    This version will not work with letters in phone numbers (e.g. 1 (123) 123-abcd). Restricting the format to only numbers reduces errors by pulling incorrect numbers. Using the expression above, you can change out dashes with spaces or periods. You can also have a plus sign at the front. The following are examples of the phone number patterns that the expression above will find. You can replace zeros with any digit 0-9.

    International:

    0 000 (000) 000-0000
    000 (000) 000-0000
    0-000-000-000-0000
    000 (000) 000-0000
    000-000-000-0000
    000-00-0-000-0000
    0000-00-0-000-0000
    +000-000-000-0000
    International formats explained

    U.S. Versions:

    0 (000) 000-0000
    +0-000-000-0000
    0-000-000-0000
    000-000-0000
    (000) 000-0000
    000-0000

    Issues You Can Expect with This Expression

    There are a few situations where this expression will fail. It’s unlikely you will meet the following case scenarios, but I want to point them out just in case. In all the cases, a dash, period, or space must precede the last four digits and that separator must have at least 3 digits in front of it (e.g. 000-0000).  You will see that pattern grayed out in all of my examples below.

    Case 1: If you are trying to pull a number that shows right after a dash, space, or plus sign, it’ll include those as part of the pattern found.

    Case 2: The version with the plus sign is inappropriate for international uses.

    Case 3: If the last four numbers of a sequence of  18 digits have a dash, then it’ll return that number with everything before it. Removing that dash will keep it from following the pattern.

    Case 4: Every dash, space, or period you add between digits in front of the last 8 digits (including a dash) will cause the expression to return the first digit and everything between it and the final eight digits up to 19 total digits, including the dashes, etc. You can enter up to 7 consecutive spaces and it’ll return the last number in front of those spaces, the spaces themselves, and the last 8 digits.

    0-000-0000
    -0-000-0000
    -0-000-000-0000
    -0 (000) 000-0000
    +000 00-0-000-0000
    3456789012345-6789
    0       000-0000

     

    JavaScript Functions that Use Regular Expressions

     

    PHP Functions that Use Regular Expressions