Validation icon indicating copy to clipboard operation
Validation copied to clipboard

how to accurately verify in different countries?

Open titlebaby opened this issue 5 years ago • 2 comments

I have doubts. Please help me. How to use it accurately in different countries? For example, how to accurately verify the mobile phone number in China? v::phone() or v::phone()->regex($match)?

titlebaby avatar May 30 '19 10:05 titlebaby

The phone rule has no way to select the country. It verifies if the provided input is a valid phone number regardless of the country. The regex rule in the other hand lets you use a regex pattern to test against, which is more suitable in your case by adding the pattern that limit which countries you would like to support. For example :

// This will ensure that the number is belonging to my country as it's the most pattern I'm sure about
v::phone()->regex('/(\+216)?(2|9|3|4|5)[0-9]{7}/')->validate($input);
// you may need to strip the special characters ( like spaces or dashed ) before validating it to simplify the RegEx pattern

The pattern is challenging though, as for many countries there's a more complicated rules on how a number is formatted. Also, I think it's pretty absurd to use both regex and phone rules as you can simply use only regex to test against the country or at least use it to for a slight verification if you find yourself testing the whole format using regex rule (like what I did in the example) just drop the phone rule.

I would like to mention that using the AllOf rule is more convenient as it will unify the error and make it easier to craft the error's message.

v::allOf(
    v::phone(),
    v::regex($pattern)
)->validate($input);

sunchayn avatar May 30 '19 12:05 sunchayn

Starting from 2.3, phone will be able to match specific countries and international numbers.

See this commit for more info: https://github.com/Respect/Validation/commit/cfccf282ac673f59d43fa34c3681fb3d0c769a83

It will not, however, be able to tell fixed number from mobile numbers apart. That might be too much for now, but if enough people ask for it, I'll expose that part of libphonenumber to our API.

alganet avatar Feb 19 '23 00:02 alganet

I'm closing this one because version 2.3 has been released, and we hope that has fixed the issue. Let us know if that's not the case so we can reopen the issue.

henriquemoody avatar Mar 09 '24 00:03 henriquemoody