How exact do you need it to be? If you just want to check that the data includes digits, possibly some spaces, dashes and a plus, you could try something simple like this:
~^[\\+0-9\\s-]{10,20}$~
(I haven’t tested that, and regEx is not my thing.)
Indeed. That’s why I asked how specific it needs to be. I’m wary of trying to lock data down too tightly. I don’t see the advantage of trying to check the input in such detail, but it’s up to you.
But , I’m not trying to check inputs , I’m just trying to extract all phone numbers from uploaded CVs to my website… It’s very important for me … And it must be as accurate as possible , To match any present number with format like one of the previously mentioned numbers , and not to match any other thing !
When accepting this kind of input from the user, I tend to strip all the dash characters, then process it. If the string contains any other non-number characters (such as “x” for extension), then I return the form back to the user with an error on the field.
So, basically, you would just check for a minimum of 11 digits, and a maximum of 13 digits. Then, add the area code as necessary and save it in your database as just numbers. Then when you need to display it back to the user, format the string with + and - characters as you see fit using substrings.
Or–have a drop-down field next to your phone number field. The drop-down would be a list of your country codes, and the user would just enter their actual number.
Or–have the country code number as a drop-down, and have a 3-digit field, a 2-digit field, a 2-digit field, and a 3-digit field. That would force the user to enter the phone number in a very specific format.
I agree with Force Flow that it is much better to control data format on input. i.e, select options vs text input.
Assuming you want to harvest old messy data - and will hopefully put new code in place to eliminate the need for resource use - pseudo-translated your string is
maybe (## or ### or +#) then (########## or ### ## ## ### or ###-##-##-###)
Welcome to the forums @gemswebs;
I am confident we can get you the answer(s) you need. There are a number of very talented and skilled people here who are anxious to help.
Until one of those talented and skilled folks comes along, let me try to help you.
In order to locate these numbers from a CSV list, try this RegExp:
Based on your success with it, we may need to make some adjustments. Also, because your pattern is very complex (the number of possible ways in which the number can be displayed) it may require a multi-pass evaluation.
Mittineague , Force Flow , It’s not a text derived from user inputs … Wt I want to do exactly is I want to extract phone numbers from uploaded CVs’ files to y site… So I can’t control how they are written …
ParkinT … Your regular expression will not do the job accurately …
\+?\d{1,2}?\d{3}[- ]?\d{2}[- ]?\d{2}[- ]?\d{3}
This will n’t match a mobile number : 0111 111 111
A mobile number will always be 11 number … Plus spaces and digits may be scattered in where between the digits.
But note that there may be prefixes or not , and spaces or dashes may be scattered anywhere between the digits , i.e. may be:
010 xx xx xxxx
OR
01 0xx xxx xxx
or any other form.
So, what makes these numbers unique from those that may appear in any other fields of the CSV? That is the key question.
A simple RegExp that searches for a string of numbers (from some minimum quantity to some maximum quantity) and ignores all dashes or spaces could be relatively simple. Would that do the trick for you?