stef
stef

Reputation: 27749

Regex for min 9 numbers

I'm trying to make a javascript regex to match:

So far I have

^[0-9-+\/\s]{9,}$

The only problem with this (I think) is that it counts the non numeric permitted characters along to reach the minimum 9.

How can I amend it so that it only counts the numbers to reach the minimum 9?

Upvotes: 7

Views: 2697

Answers (4)

Civa
Civa

Reputation: 2176

its simple with look-ahead

Try this pattern:

^(?=([^\d]*\d){9,})[0-9-+\/\s]*$

Upvotes: 0

Anirudha
Anirudha

Reputation: 32787

You can use lookahead to check if there are 9 or more digits anywhere

^(?=(\D*\d){9,})[\d/+ -]+$
 --------------
         |
         |->match further only if there are 9 or more digits anywhere

OR

^([/+ -]*\d){9,}[/+ -]*$

Upvotes: 3

Fred Foo
Fred Foo

Reputation: 363527

If you want to solve this in a single RE (not necessarily recommended, but sometimes useful):

^[-+\/\s]*([0-9][-+\/\s]*){9,}$

Or, if you want the first and last characters to be digits:

^[0-9](^[-+\/\s]*[0-9]){8,}$

That's: a digit, followed by eight or more runs of the optional characters, each ending with a digit.

Upvotes: 8

nothrow
nothrow

Reputation: 16168

^([0-9][-+\/\s]*){9,}$ should do.

Upvotes: 1

Related Questions