Ron
Ron

Reputation: 1

phone number regex not working

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})$/;

Upvotes: 0

Views: 597

Answers (2)

Ian Hazzard
Ian Hazzard

Reputation: 7771

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.');
}

Upvotes: 0

Pointy
Pointy

Reputation: 413682

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)$/;

Upvotes: 1

Related Questions