The format for E.164 phone numbers is:
[+][country code][phone number plus area code]
The format is simply a plus sign followed by a series of numbers. E.164 can have a maximum of fifteen digits.
The first thing we see in the function is the constant regEx, which is the regular expression that defines the parameters for our validation. The regEx statement states that all phone numbers must begin with a plus sign. After the plus sign, every entry must have at least ten digits with a value from one through nine, but cannot have more than fourteen digits.
Once we have defined the rules, we can use the test statement to compare the value held in the variable phoneNumber against those rules. If the value follows the correct format, the test will return true. Otherwise, it will return false.
Now that we’ve seen how the function is supposed to work, let’s give it a try with some cases to see how the code reacts.
In Example 2, we submit the phone number +12125551212. This number features a plus sign followed by a series of eleven numbers. Since this follows the format that we set up with regEx, the test statement allows the number and returns true. This entry could be an E.164 formatted phone number.
In Example 3, we submit 12125551212. Example 3 is similar to example 2, but it is missing the opening plus sign. Because it does not have a plus sign, it does not meet the requirements set up in regEx, so the test statement fails it and returns false. This entry is not an E.164 formatted phone number.
In Example 4, we submit 2125551212. This example is like the last one and also doesn’t include the opening plus sign. Without the opening plus sign, the test statement will return false. This entry is not an E.164 formatted phone number.
In Example 5, we submit +1-212-555-1212. This example has an opening plus sign, but the numbers that come after include dashes. The only characters allowed after the plus sign are numbers according to regEx. The test statement will fail this number because of the dashes and will return false. This entry is not an E.164 formatted phone number.
In Example 6, we submit +1 212 555 1212. This example gets rid of the dashes from the last example, but unfortunately, empty spaces are not allowed either, and the test statement will fail this number and return false. This entry is not an E.164 formatted phone number.
In Example 7, we submit +1 (212) 555-1212. This example uses a common way that people write a phone number. Unfortunately, neither parentheses, spaces, nor dashes are usable, and the test statement will return false. This entry is not an E.164 formatted phone number.
I hope that you’ve enjoyed reading over these examples and can see how useful regular expressions can be for making sure you have correctly formatted phone numbers before sending them to your database. We recommend using this approach any time you are using E.164 phone numbers, and we recommend using techniques similar to this to validate every field in your forms.