Udzzzz
Udzzzz

Reputation: 155

Regex pattern number question for phone number

What is the regex pattern for:

I tried /^[1-9][x-1][x-+]{+10}$/ but it's showing me an error.

Upvotes: 2

Views: 83

Answers (3)

Shiva Bala Rakavi
Shiva Bala Rakavi

Reputation: 31

RegEx 1: /^(?![0][1][+])[2-9]([0-9]){9}$/

RegEx 1 explanation:

(?![0][1][+]) Cannot start with a '0', '1' or '+' [ Negative lookahead ]

[2-9] Only numbers are allowed [ So the first digit would be one of 2-9 and not 0 or 1 or + ]

([0-9]){9} Must be to be 10 digits long [ The remaining {9} digits can be any number between 0 to 9 ]

RegEx 2: /^[2-9]([0-9]){9}$/

RegEx 2 explanation:

[2-9] Cannot start with a '0', '1' or '+' and Only numbers are allowed. So the first digit can be only a number between 2-9

([0-9]){9} Must be to be 10 digits long [ The remaining {9} digits can be any number between 0 to 9 ]

RegEx 3: /^[2-9]\d{9}$/

RegEx 3 explanation:

[0-9] can be represented as \d. So changing it in RegEx 2, we will get /^[2-9]\d{9}$/, as already mentioned in the other answer

For the first character, it is not sufficient to confirm the non-existence of 0,1 and + , it should be verified to be a number as well.

Upvotes: 0

TFK trans
TFK trans

Reputation: 25

var str=prompt();
if(/^[^+01]\d{9}$/i.test(str))
    alert("true");
else
    alert("false");

Upvotes: 1

Addis
Addis

Reputation: 2530

let reg = /^[2-9]\d{9}$/g;

console.log(reg.test('9123456789'))

Upvotes: 2

Related Questions