Regex for validating international phone numbers

11-Nov-2019 22:35

Here i provide a regex pattern to determines if the phone number is in correct format, the pattern force starting with 3 digits follow by a “-” and 7 digits at the end.

Explanation \d = only digit allow = length All phone numbers must in “xxx-xxxxxxx” format.

There is another form of regular expression that PHP supports (POSIX extended), but I’m not going to touch on that at all.

Consider POSIX extended regular expressions the weaker cousin of PCRE (I’ll likely be flamed for this).

It is crucial to the integrity of data and to the security of an application to validate any input.

I believe that most pass off validation out of laziness, but, as you can see, validating a phone number only adds a few extra lines of code to your application.

regex for validating international phone numbers-31regex for validating international phone numbers-81regex for validating international phone numbers-65

Take a text file of customer data from an electric utility, for example.

You can now take each of these elements and format the number as you wish so that all the numbers in the database will follow the same pattern.

Likewise, you could have a free form field – rather than three separate fields – on a Web page for users to enter a phone number.

When relying on your end-user to supply information in the proper format, you’re S. Most of the applications I work with have a Web-based front-end where users submit data.

Follow me as I take a look at validating user-entered data in this first installment of what I hope to be a continuing series.

Take a text file of customer data from an electric utility, for example.

You can now take each of these elements and format the number as you wish so that all the numbers in the database will follow the same pattern.

Likewise, you could have a free form field – rather than three separate fields – on a Web page for users to enter a phone number.

When relying on your end-user to supply information in the proper format, you’re S. Most of the applications I work with have a Web-based front-end where users submit data.

Follow me as I take a look at validating user-entered data in this first installment of what I hope to be a continuing series.

Before we do that, though, let me digress and explain what’s going to happen in this validation process.