Validating phone numbers
Don’t. That’s right: don’t validate phone numbers!
Perhaps you want to check, not what a phone number looks like, but rather that the user can access the phone they are telling you they own. If that’s what you needed: send a text or a call with a code to verify the account. As a bonus, your user and your system will then be typo-proof.
So then again, don’t validate phone numbers. You can check it isn’t blank (empty or filled with space/tab characters), but just keep the format people want you to store:
- France likes spaces or dots, groups of two 01.23.45.67.89
- UK likes groups of three 01 23 456 789
- services in the US like mnemonics like 01234-PIZZA
- you might later want special or foreign numbers
Validating numericality will remove all of these:
- 01 23 45 67 89 -> 0123456789 (legibility)
- 0800 123 123 -> 0800123123 (123.123 is more memorable than 12.31.23)
- 0800 42 42 42 -> 0800424242 (0800 means free number, French people like it better than 08.00)
- 0800 1 PIZZA -> KO or 0800174992 (there are gems to find mnemonics IIRC)
- a shorter phone number that’s inside a company’s office -> KO
- foreign number: +49 (0) 123456789 -> KO
- special numbers like 911 -> KO
I don’t always say that, but: trust people, not code ;)