regex to validate phone number - php

Help me write a regex for below conditions
number can start with +
number can contain - or . but not () and /
number can start with 0
Min number in the string should be 9 digits excluding extension details and starting +
max number in the phone number field should not reach more than 14 excluding +
if the string contains ex/ext/x then the digit after should not have more than 5 characters (normally 4)
this above should satisfy examples below
0-1234-123456
+91-1234-56789012
+91-1234-56789012 x1234
+91-1234-56789012 ex1234
+91-1234-56789012 ext12345
+91-1234-56789012x1234
+91-1234-56789012ex1234
+91-1234-56789012ext12345
91-1234-56789012
91-1234-56789012 x1234
91-1234-56789012 ex1234
91-1234-56789012 ext12345
91-1234-56789012x1234
91-1234-56789012ex1234
91-1234-56789012ext12345
91123456789012
91123456789012 x1234
91123456789012 ex1234
91123456789012 ext12345
91123456789012x1234
91123456789012ex1234
91123456789012ext12345
91.1234.56789012
91.1234.56789012 x1234
91.1234.56789012 ex1234
91.12345.6789012 ext12345
91.12345.6789012x1234
91.12345.6789012ex1234
91.12345.6789012ext12345
1-234-567-8901
1-234-567-8901 x1234
1-234-567-8901 ext1234
1 234 567-8901
1.234.567.8901
12345678901
I found few links online one of them is
http://ericholmes.ca/php-phone-number-validation-revisited/
and on stackoverflow
A comprehensive regex for phone number validation
also
^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$
is not working for many of the above

^\+?(\d[.\- ]*){9,14}(e?xt?\d{1,5})?$
Explanation;
^ Asserts start of string
\+? Matches an optional plus
(\d[.\- ]*){9,14} between 9 and 14 single digits, possibly seperated by spaces, dots, or dashes.
(e?xt?\d{1,5})? Optionally a x possibly preceeded by an e or followed by a t. The letters always followed by between 1 and 5 numbers.
$ Asserts end of the string

This will do it, but depending on which language you are programming in (we always need to know that with regexs, so if this doesn't work for you, reply with the language used. I've tested it in PHP5.)
Your condition 5 (max 14 chars in the phone no) appears to be in error, since several of your examples contain 16 characters if they include dots or hyphens. In any case, this does not check for overall length of the whole thing because of the other length checks it does; it would need a second regex, or, better, check the string length beforehand (eg in PHP by doing a call to strlen).
You might want to allow for a space in extension numbers, eg ext 1234; if so add \s* in the appropriate place.
I hope this helps.
^\+?\d[\d-\.\s]{8,15}\s?((ext|ex|x)\d{3,5})?$

Related

Need help for a regular expressions with PHP Preg_match to validates a number field on our form

:)
We would like to set a special condition (based on PHP Preg_match regular expression) to validates a number on our form.
That “number field” need, at first, only contain a max of 13 numbers (and only numbers. No letters or anything else).
The very first number need to be (only) “1” or “2” (not anything else)
The 4rd and 5rd number represent (the 2 numbers combinated) the “Month of birth” of someone, so the 4rd number need to be "0" or "1", and the 5rd need to be between "1" and "9".
Really appreciates if you can help us for that, to have the good “syntax” for the regular expression in PHP Preg_match to validates that field on our form! :)
Thanks to the community for your support and help!
Regards
Here is the literal regex pattern you have described to us:
^[12]\d{2}(?:0[1-9]|1[0-2])\d{8}$
Sample script:
$input = "1231212345678";
if (preg_match("/^[12]\d{2}(?:0[1-9]|1[0-2])\d{8}$/", $input)) {
echo "MATCH";
}
This regex pattern says to:
^ from the start of the string
[12] match 1 or 2 as the first digit
\d{2} then match any digits in the 2nd and 3rd position
(?:0[1-9]|1[0-2]) match 01, 02, ..., 12 as the two digit month
\d{8} then match any other 8 digits
$ end of string

PHP - Regex on numbers, optional decimals and letter

I'm trying to "validate" some input from a form on the backend but I kinda got stuck with the required regex. Basically, I want to get a number, no leading zeroes, with an optional decimal and an optional case insensitive thousands notation (1,000 = 1k, 1,000,000 = 1m, 1 billion = 1b ...)
These should match/validate:
12.39
12.389k
99.1003b
40.1m
These should fail.
0.0
12.00b
1e3
2^5
0xFF
12.6z
asdf
So far I've tried this:
^[1-9]\d*(?:\.\d+)?(?:[kmbt])?$
It works but I'm thinking maybe someone could solve this a bit more elegantly than I did.
[Edit] without thousand separator:
/^(?:[1-9][0-9]*(?:\.[0-9]*[1-9])?|0\.[0-9]*[1-9])[kbmt]?$/i
demo
[Old answer] with thousand separator:
You can do it with:
/^(?:[1-9][0-9]{0,2}(?:(,?)[0-9]{3})?(?:\1[0-9]{3})*(?:\.[0-9]*[1-9])?|0\.[0-9]*[1-9])[kbmt]?$/i
demo
details:
^
(?: # numbers >= 1
[1-9][0-9]{0,2}
(?:(,?)[0-9]{3})? # capture an eventual thousand separator
(?:\1[0-9]{3})*
# decimal
(?:\.[0-9]*[1-9])?
| # numbers < 1
0\.[0-9]*[1-9]
)[kbmt]?$
Update in REGEX.
Below regex will not leading zero allow special characters and characters without specified for notation.
Below regex will allow decimal and integer with thousand and other notation.
^[1-9]\d*(\.[0-9]+)?(?:[kmbtKMBT])?$
Please check working demo : http://www.regexpal.com/?fam=95995
The below should be what you're after:
/(\b[1-9][\d\,\.]+([kmbt]?))(\s)/g
It matches 12.00b however... Here is an example of it's use.
Group 1 will contain your exact match.

Stripping down Phonenumber (mobile)

Is there a function or a easy way to strip down phone numbers to a specific format?
Input can be a number (mobile, different country codes)
maybe
+4917112345678
+49171/12345678
0049171 12345678
or maybe from another country
004312345678
+44...
Im doing a
$mobile_new = preg_replace("/[^0-9]/","",$mobile);
to kill everything else than a number, because i need it in the format 49171 (without + or 00 at the beginning), but i need to handle if a 00 is inserted first or maybe someone uses +49(0)171 or or inputs a 0171 (needs to be 49171.
so the first numbers ALWAYS need to be countryside without +/00 and without any (0) between.
can someone give me an advice on how to solve this?
You can use
(?:^(?:00|\+|\+\d{2}))|\/|\s|\(\d\)
to match most of your cases and simply replace them with nothing. For example:
$mobile = "+4917112345678";
$mobile_new = preg_replace("/(?:^(?:00|\+|\+\d{2}))|\/|\s|\(\d\)/","",$mobile);
echo $mobile_new;
//output: 4917112345678
regex101 Demo
Explanation:
I'm making use of OR here, matching each of your cases one by one:
(?:^(?:00|\+|\+\d{2})) matches 00, + or + followed by two numbers at the beginning of your string
\/ matches a / anywhere in the string
\s matches a whitspace anywhere in the string (it matches the newline in the regex101 demo, but I suppose you match each number on its own)
\(\d\) matches a number enclosed in brackets anywhere in the string
The only case not covered by this regex is the input format 01712345678, as you can only take a guess what the country specific prefix can be. If you want it to be 49 by default, then simply replace each input starting with a single 0 with the 49:
$mobile = "01712345678";
$mobile_new = preg_replace("/^0/","49",$mobile);
echo $mobile_new;
//output: 491712345678
This pattern (49)\(?([0-9]{3})[\)\s\/]?([0-9]{8}) will split number in three groups:
49 - country code
3 digits - area code
8 digits - number
After match you can construct clean number just concatnating them by \1\2\3.
Demo: https://regex101.com/r/tE5iY3/1
If this not suits you then please explain more precisely what you want with test input and expected output.
I recommend taking a look at LibPhoneNumber by Google and its port for PHP.
It has support for many formats and countries and is well-maintained. Better not to figure this out yourself.
https://github.com/giggsey/libphonenumber-for-php
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$usNumberProto = $phoneUtil->parse("+1 650 253 0000", "US");

PHP regex match multiple pieces

I am new to regex and I know the basics of how to pull out one sub string from a given string but I am struggling to get out multiple parts that I need. I am wondering if someone could help me with this simple example and then I work my way from there. Take this string:
LMJ won Neu. Zone - KEN #55 LEIGH vs LMJ #63 ONEIL
The parts in italics are the parts of the string that will change and bold will stay the same in every string. The parts I need out are:
First team id which in this case is LMJ, this will always start the string and be 3 uppercase letters, ^[A-Z]{3}?
The Neu part which could be one of 3 strings, Neu, Off, Def, [Neu|Off|Def]?
The second team part which will come always after the word Zone -, [A-Z]{3}?
Need the numeric part of the string after the first #. This could be 1 or 2 digits [0-9]{1,2}?
5.Third team part same as 3 except will appear after vs, [A-Z]{3}?
Same as 4 need numeric part after 2nd #, [0-9]{1,2}?
I would like to put that all together into one regex is that possible?
Everything inside square brackets is a so-called character class: it matches only a single character. so, [Neu|Off|Def] means: exactly one of the characters N, e, u, |, O, f or D (repetitions are ignored)
What you want is a capture group: (Neu|Off|Def)
Putting it together:
^([A-Z]{3}) won (Neu|Off|Def)\. Zone - ([A-Z]{3}) #([0-9]{1,2}) [A-Z]+ vs ([A-Z]{3}) #([0-9]{1,2}) [A-Z]+$
(This assumes you're not interested in the "LEIGH" and "ONEIL" parts, and these are always in upper case letters)
The regex should be something like;
'/([A-Z]{3})\ won\ (Neu|Off|Def)\.\ Zone\ -\ ([A-Z]{3})\ (\#[0-9]{1,2}\ \w+)\ vs\ ([A-Z]{3})\ (\#[0-9]{1,2}\ \w+)/'
() are used for capturing the different parts.
This is not tested properly.

PHP Dynamic Regular expression?

I'm relativly new to regular expressions but I managed to create a working expression to validate dates (without leap years, and assuming people enter a valid 30 or 31 digit for a month).
This is the expressen:
/^\d[1-31]{2}\-\d[1-12]{2}\-\d[1900-2009]{4}$/
But I would like to have a dynamic regular expression like:
$yearSpan = (date("Y") - 110)."-".date("Y");
/^\d[1-31]{2}\-\d[1-12]{2}\-\d[$yearSpan]{4}$/
When I try to use this expression it keeps telling me the compilation failed because a range out of order in character class.
I'm using this expression to validate dates of birth, and it would be nice not to update it every time a year passes by.
Any suggestions?
I think you should consider using some date/time functions for this purpose.
You're using the character classes in wrong way. [1-31], [1-12] and [1900-2009] will not check for the ranges you have in them. That's not how character classes work and hence that's not how you check for numeric ranges with regex.
A character class [a-r] matches any char from a to r. [a-rx] matches any character from a to r and the character x. Similarly, [1-39] matches any character from 1 to 3 and the character 9 - hence it matches one of 1,2,3 and 9 - and not any number from 1 to 39 as you intended it to.
[1-31]{2} matches two consecutive numbers (the last 1 is redundant), both in the range 1 to 3 - 33 is a valid match.
To match a number from 1 to 31, the correct regex is 0?[1-9]|[1-2][0-9]|3[0-1]. ('0?' takes care of zero padding as in 01-09-2009
For months: 0?[1-9]|1[0-2]
For year: 19[0-9]{2}|200[0-9]
And - is not a meta character outside character classes - you need not escape it.
The correct regex would be:
/^(0?[1-9]|[1-2][0-9]|3[0-1])-(0?[1-9]|1[0-2])-(19[0-9]{2}|200[0-9])$/
If you are not interested in capturing the values, you can write it as:
/^(?:0?[1-9]|[1-2][0-9]|3[0-1])-(?:0?[1-9]|1[0-2])-(?:19[0-9]{2}|200[0-9])$/
You can do it with PHP date and time functions
http://php.net/manual/en/function.checkdate.php

Categories