Theo Jouret
Theo Jouret

Reputation: 113

RegExp for Phone Number

I am trying to use RegExp() to accept the following inputs:

What I have been trying to use is:

([0-9]{2,2}[ |-|.]?){4,4}[0-9]{2,2}

Which I understand as: (digit 2 times, followed by space or - or . or nothing) 4 times, then digit 2 times.

I have been testing [0-9]{2,2} which doesn't even behave as I expected since it accepts at least 2 digits, and not exactly 2 digits.

Upvotes: 0

Views: 77

Answers (2)

sp00m
sp00m

Reputation: 48817

This one should suit your needs (last case not matched as expected):

^\d{2}([ .-]?)\d{2}(?:\1\d{2}){3}$

Demo

Upvotes: 1

Sagar V
Sagar V

Reputation: 12478

var mob=/^([0-9]{2}(\s|\.|\-)){4}[0-9]{2}$/;
console.log(mob.test("12.34.56.78.90"));
console.log(mob.test("12-34-56-78-90"));
console.log(mob.test("12 34 56 78 90"));

var mob=/^([0-9]{2}(\s|\.|\-)?){4}[0-9]{2}$/;
console.log(mob.test("1234567890"));
console.log(mob.test("12 34 56 78 90"));

Upvotes: 1

Related Questions