0

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

4
  • Please add sample data showing what these mobile numbers look like. Commented May 18, 2019 at 8:13
  • Apologies, one mobile number is "0750617965789" Commented May 18, 2019 at 8:14
  • Do the phone numbers always consist exclusively of digits? Commented May 18, 2019 at 8:17
  • Some begin with + Commented May 18, 2019 at 8:18

2 Answers 2

1

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

More info from OP, update to ^\+*(\d){9,}$ to include the optional plus sign
@JasonB That pattern is incorrect, and would allow for any number of leading +. I'm pretty sure the OP only expects at most one +.
Right you are, should be ? instead of *. ^\+?(\d){9,}$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.