


Then on the backend you would essentially strip out all the non-numeric characters from the subscriber number and combine the country code and stripped "subscriber number" into an E.164 format number and store that in the database. Then have everything other than the country code (generally called a "subscriber number") added to another input. The "best" solution is to separate country code into a different field or input. There are too many combinations and expectations built into these conventions. Phone numbers are like time zones, you'd think they're easy to deal with but they're surprisingly finicky to get right.Ī truly global phone number regex is quite literally impossible to make. With something as complex as phone numbers I'd just stick to using standard form validation code (like in the HTML standard) to warn users of explicitly invalid input like letters or most special characters and storing phone numbers as a 20 character random text strings for all other purposes. Even if you're only targeting American customers, you'll probably need to support the phone number format for a visiting foreigner as well. Many prebuilt phone number input boxes have trouble with even normal American phone numbers, let alone foreign phone number systems. You also need to strip spaces and parenthesis but keep other characters such as + and ~. You'll probably need to do more than just strip dashes, though. As far as native components go, I think most platforms actually have a prebuilt control for this that works pretty well in my experience.

I was convinced that this already existed in the form of masked inputs, but after trying I realised those are broken in mobile Chromium and utterly unusable in mobile Firefox.
