Rowandinho
Rowandinho

Reputation: 203

regex digits formatting

I want to use regex specifically! to check if a mobile number contains 9 or more digits. I am a little unsure as to how to format this exactly.

I want to check inside an if statement like the below

if(mob >= (.{9})

This is clearly not correct, any help would be great

Upvotes: 0

Views: 46

Answers (2)

Maheer Ali
Maheer Ali

Reputation: 36594

You need to check of numbers not for all the characters. And also use test() to check if string matches regex or not.

const testMob = str => /^\+?[0-9]{9,}$/.test(str);

console.log(testMob('+123456789')) //true
console.log(testMob('+1234567890')) //true
console.log(testMob('+133333'))  //false

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522712

Use test with the regex pattern ^\+?[0-9]{9,}$:

var number = "+123456789";
if (/^\+?[0-9]{9,}$/.test(number)) {
    console.log("MATCH");
}

Upvotes: 1

Related Questions