0

I have a form with an input that follows this pattern:

pattern='(\+|00)\d{2,3}[-]\d{8,10}'

an example would be +999-123456789

I have to form validate it again using javascript and have tried to convert the pattern into a Regex, the example passes the pattern but fails passing the regex. Any idea as to why?

var check = /^([00|+])([0-9]{2,3})[-]?([0-9]{8,10})$/;
murgatroid99
  • 17,055
  • 9
  • 55
  • 92
Ron
  • 1
  • 1
  • 2
    Why don't you do the same check? `/^(\+|00)\d{2,3}-\d{8,10}$/.test('+999-123456789'); // true` – Paul S. May 29 '13 at 20:22
  • http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation – Dave Jarvis May 29 '13 at 20:22
  • http://jsfiddle.net/RaKZg/ your regex seems to pass your sample string, although as @Pointy mentioned, it wont work with double zeroes leading – Smern May 29 '13 at 20:27

2 Answers2

1

Your regular expression is incorrect. This:

[00|+]

is equivalent to

[0|+]

and means "match a single character that's either '0', '|', or '+'." I think you want:

var check = /^(00|\+)(\d{2,3})-(\d{8,10)$/;
Pointy
  • 389,373
  • 58
  • 564
  • 602
0

Here is your pattern tranferred to a RegEx: /(\+|00)\d{2,3}-{0,1}\d{8,10}$/. Example below.

var number = '+999-123456789';

if (number.match(/(\+|00)\d{2,3}-{0,1}\d{8,10}$/)) {
  alert('Phone number valid!');
} else {
  alert('Phone number invalid.');
}
Drazzah
  • 7,371
  • 7
  • 31
  • 57