JavaScript E164 phone number validationWhen you need to ensure that the user inputs an E164 compliant phone number, JavaScript E164 phone number validation helps to minimize errors.

I this article you will learn how to set up a JavaScript E164 phone number validation system for your forms. E.164 is the international telephone numbering plan, and it ensures each device has a unique number. This system allows you to phone or send text messages anywhere in the world.

E.164 Format

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.

Validation Function

The easiest way to perform JavaScript E164 phone number validation is with an ordinary function that contains a regular expression and a test statement. We’ll call our function validatePhoneForE164 and it will pass a variable named phoneNumber to be validated. We usually get the phoneNumber variable through a form entry.

The JavaScript E164 phone number validation function would look very similar to Example 1.

Example 1

Regular Expression

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.

Test Statement

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.

Examples

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.

Example 2

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.

Example 3

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.

Example 4

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.

Example 5

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.

Example 6

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.

Example 7

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.

Conclusion

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.

If you have found these examples helpful and informative, please share this JavaScript E164 phone number validation technique on Facebook and Twitter.

Leave a Reply

Your email address will not be published. Required fields are marked *