I need to verify a form, and in this I have a field to enter phone numbers.
According to wikipedia the Argentine telephone numbers, I quote:
Area codes can be 2, 3, or 4 digits long, with the remaining 6, 7, or 8 digits being the local phone number.
For example, the number
+54 11 1234-5678
for Buenos Aires is made up of a 2-digit area code followed by an 8-digit telephone number; while it+54 341 123-4567
would be an example of a Rosario number.
In short, you would need a regular expression that covers all those cases. And I would need to add a rule to it, which would be that the +54
is optional.
At the moment I am using this expression as a last option:
/^\(?\d{2}\)?[\s\.-]?\d{4}[\s\.-]?\d{4}$/
but it's not what I'm looking for since this allows (11) 1234 3434
or 1123243434
, etc. I would need to add the rules explained above for example (353) 123 4345
and make +54
it optional
We can choose to validate in 2 ways:
1. Simple way: remove everything that is not a number.
The direct way to check if an Argentine phone number is valid is to remove from the string everything that is not a number and validate that it has 10 digits (plus prefixes as optional).
(does not take a local number without an area code as valid
4444-0000
)Code:
2. Analyze each part of the number
If instead we are interested in validating more exhaustively but, at the same time, extracting each of the parts that make up the number, then we go with a much longer and stricter regex:
Don't be fooled by the length of the regex: it's basically a repeat of the optional separators (parentheses, spaces, hyphens or periods) verifying that they are in one of the formats that we normally use. Just because it's a long regex doesn't make it any less efficient.
Explained with the Debuggex diagram (open the image in another tab to see 100%):
Characteristic:
+54
,0054
or54
.11
(+ 8 digits)2xx
and3xx
(+ 7 digits), or2xxx
and3xxx
(+ 6 digits)06xx
and08xx
(+ 7 digits, only if0
or was used54
)15
cell phone after area code (if not used9
after international code).-
,.
,-
and.
(optional, but only in valid places).11-4-333-0000
The advantage of this expression is that it allows us to use groups to return each part separately. That is, we can take the result of the function as true/false, or assign it to a variable and get each part of the number separately.
Code
Example:
*Note: if no separators were used in the number, it could misseparate into
area
a 3-digit area code such as343
(Paraná), instead of taking the 4-digit one3436
(Victoria). But that6
one is not lost, it appears as the first digit oflocal_1
, and innumero
you get the3436xxyyyy
... New area codes are created over time, and the only way to properly separate it would be to compare against a continually updated listing . However, for most use cases, it is usually not relevant to separate that digit well by distinguishing whether it is a 4-digit area code or part of the local number.Tests
>>> Demo in Ideone and in regex101
The regex explained (only for fans)
Alternatives:
I prefer this type of manual validation for this case, but it may be easier for you to use one of these solutions:
libphonenumber from Google (Port to PHP: libphonenumber for PHP - demo ).
Twilio Lookup (REST API that requires you to register - queries to validate and format are free) - code sample .
intl-tel-input for JavaScript (jQuery plugin with form validation using libphonenumber) - cdnjs - tutorial
Or you can download the complete list of assigned numbers from the ENACOM website and check that the callsign and its block exist.