Phone Number Validation: A Cautionary Tale
We recently received a ticket for a project requesting phone number validation in some forms. Seems like a simple enough problem to solve, right?
Well, it’s not. Even with existing libraries that claim to handle phone validation, the more general the validation becomes, the more cumbersome it gets for both developers and users.
The library we attempted to implement, for example, required users to select a country code before entering their phone number.
However, if a user enters the country code with the phone number, the result is invalid because the library adds the country code before validating.
Of course at a certain point, you can’t be too concerned about things like users’ formatting preferences or whether or not extensions should be allowed if you want to ensure phone numbers are valid.
But the point is, before realizing we would have to rewrite a core feature of a third-party software to implement validation on a handful of other forms, 3 days had been wasted trying to implement basic phone number validation.
So the lesson of the story is: if you’re going to go crawling down a rabbit hole, see if you can find a map of it first.