Validating phone numbers in php
Several months ago, when I was first getting familiar with regular expression, I made a post talking about PHP form validation for phone numbers and email addresses.
A couple of days ago, I revisited that post and realized how inefficient my function actually was.
The expression looks like this: Now that we have an expression that will work some magic and validate a phone number, let’s see how to put it into action.
There is another form of regular expression that PHP supports (POSIX extended), but I’m not going to touch on that at all.
Once that’s done, I can put them back together in a unified format for entry into a database.
Here’s how to use the regular expression to do just that: will be “4321.” Go ahead and try the code with any US formatted number you can think of. If not, let me know; I’m always willing to revise my code.
Other applications, also Web-based, involve data that has been imported from some other source, such as a text file. There are two places to validate data: the client-side and server-side.
In both situations, the data that the application gathers is unreliable at best. The client-side is a good choice since you’re able to check the data even as the user enters it, but you cannot rely on the user’s browser to work correctly.
Take a text file of customer data from an electric utility, for example.