admin管理员组文章数量:1300198
I'm having some trouble with a regular expression for phone numbers. I am trying to create a regex that is as broad as possible for european phone numbers. The phone number can start with a + or with two leading 0's, followed by a number in between 0 and 40. this is not necessary however, so this first part can also ignored. After that, it should all be numbers, grouped into pairs of at least two, with a whitespace or a - inbetween the groups.
The regex I have put together can be found below.
/((\+|00)+[0-4]+[0-9]+)?([ -]?[0-9]{2,15}){1,5}/
This should match the following structures
0031 34-56-78
0032123456789
0033 123 456 789
0034-123-456-789
+35 34-56-78
+36123456789
+37 123 456 789
+38-123-456-789
...
What it also matches according to my javascript
+32 a54b 67-0:
So I must have made a mistake somewhere, but I really can't see it. Any help would be appreciated.
I'm having some trouble with a regular expression for phone numbers. I am trying to create a regex that is as broad as possible for european phone numbers. The phone number can start with a + or with two leading 0's, followed by a number in between 0 and 40. this is not necessary however, so this first part can also ignored. After that, it should all be numbers, grouped into pairs of at least two, with a whitespace or a - inbetween the groups.
The regex I have put together can be found below.
/((\+|00)+[0-4]+[0-9]+)?([ -]?[0-9]{2,15}){1,5}/
This should match the following structures
0031 34-56-78
0032123456789
0033 123 456 789
0034-123-456-789
+35 34-56-78
+36123456789
+37 123 456 789
+38-123-456-789
...
What it also matches according to my javascript
+32 a54b 67-0:
So I must have made a mistake somewhere, but I really can't see it. Any help would be appreciated.
Share Improve this question edited Oct 24, 2011 at 12:23 Michiel Standaert asked Oct 24, 2011 at 12:02 Michiel StandaertMichiel Standaert 4,1767 gold badges29 silver badges50 bronze badges 9-
1
Just a small note: You use
+
in places where you mean "one occurrence".+
however means "at least one". So you might want to consider erasing all+
in the first part of the regex, as every single group in there should occur exactly once and the following?
will then make the whole thing optional. – Till Helge Commented Oct 24, 2011 at 12:07 -
Use
\s
instead of putting whitespace directly – Shameer Commented Oct 24, 2011 at 12:08 -
It's the
[0-9]{2,15}
that matches, the first regex part being optional. As the answers say, you wanted to constraint the match to the whole string (start end assertions). – mario Commented Oct 24, 2011 at 12:08 -
1
EU numbers start with 3 or 4 (except for Faroe Islands(298) and Greenland(299)) and some countries got 3 numbers in there prefix for country code so i expanded on the regex a bit and made it so it only had 2 capture groups (prefix and number)
^((?:00|\+)(?:[34][0-9]{1,2}|298|299))?[ -]?((?:[0-9]{2,15}[ -]?){1,5})$
– Blem Commented Oct 24, 2011 at 12:38 -
1
here is an example:
if (ereg ("^((?:00|\+)(?:[34][0-9]{1,2}|298|299))?[ -]?((?:[0-9]{2,15}[ -]?){1,5})$", $var, $regs)) { $countrycode = $regs[1]; $number = $regs[2]; }
(sorry it dos not look good in ments) – Blem Commented Oct 24, 2011 at 13:05
3 Answers
Reset to default 10The problem is that you don't use anchors ^ $
to define the start and ending of the string and will therefore find a match anywhere in the string.
/^((\+|00)+[0-4]+[0-9]+)?([ -]?[0-9]{2,15}){1,5}$/
Adding anchors will do the trick. More about these meta characters can be found here.
Try this, may be can help you.
if (ereg("^((\([0-9]{3}\) ?)|([0-9]{3}-))?[0-9]{3}-[0-9]{4}$",$var))
{
$valid = true;
}
Put ^
in the beginning of the RegExp and $
in the end.
本文标签: javascriptPHP regular expressions (phonenumber)Stack Overflow
版权声明:本文标题:javascript - PHP regular expressions (phonenumber) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741659290a2390940.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论