Follow

Is there a comprehensive guide on validating phone inputs gracefully in an input field (for all the countries in the world)?

Every country seems to have a different number of digits for the actual phone number, different style of writing, different length of country code (1, 2, or 3 digits), and optionally featuring an extension code too.

To an extent, regulation can affect this too over time. Maybe one country decides they want to add (or remove) an extra digit because reasons (unlikely and infrequent, but you know, edge cases).

@celia i think the only true validation is to text/call it.

but for reliably getting accurate phone numbers globally, the following three items are significant to appear in the UI:

1. the flag emojis
2. +(country code)
3. country name

a hint for the other digits are helpful too

xxx-xxx-xxxx

@tychi yeah, I've seen xxx-xxx-xxxx in the US context, but we just go xxxxxxxxxx in India. Idk if there is even an official guide on this internationally, or it's just culture in each country dictating it, even regardless of any official guideline. That would be hard to capture.

I'd love an input that is able to suggest an apt placeholder once a country is selected.

@celia @tychi I am using this one in my application

regexr.com/5m544

Dont remember where i found it (somewhere on stack owerflow). It worked for my needs so i saved it...

@celia @tychi Oh, do you mean to include the style of writing them, not just what the valid digits are? In that case, no, it’s just whatever people happen to make up. There is an official standard (E.163 from memory?), but it’s way more than narrow.

@celia Sorry if it's not what you're looking for or I misunderstood the issue, but have you tried using something like gitlab.com/catamphetamine/libp or do you want to implement it from scratch?

@celia Something tells me this is a deep neverending rabbit hole of terror just like timezones and names.

Yours sincerily. The guy with 37 letters in his name, and one of them being a Þ. Also the guy that lives in an area where the clocks were offset by 30 minutes from the capital region not that long ago.

@celia ...with that all said, of these infinite rabbit holes of terrors, the phone number one is probably the shortest one. If that even makes sense.

On a similar note, this whole thread does remind me of this great essay on name validation. kalzumeus.com/2010/06/17/false

@celia I think it’s a lost cause, but you may find some hints in this Google’s phone validation library: github.com/google/libphonenumb

I had to write a validation regex for a major Belgian phone provider once. I can’t find it back, but I had to find and read the official Belgian phone number spec, and it was not an easy task.

@meduz Thanks! Folks here and on Twitter mentioned this. Hooray for Big Tech. :)

Sign in to participate in the conversation
Fosstodon

Fosstodon is an English speaking Mastodon instance that is open to anyone who is interested in technology; particularly free & open source software.