Lucas Bustamante
Lucas Bustamante

Reputation: 17178

Regular Expression that might contain 8 or 9 numbers - how to do?

I use this regular expression to validate brazilian cellphones:

/^(\([0-9]{2}\))\s([9]{1})?([0-9]{4})-([0-9]{4})$/

Output: (31) 8876-3234 VALID

But Sao Paulo state has 9 digits cellphones:

Output SP: (31) 98876-3234

Current regex needs tweak to accept that as valid too, any tips on what should I change in it?

Upvotes: 0

Views: 149

Answers (1)

Rohit Jain
Rohit Jain

Reputation: 213223

You can use {m,n} quantifier to create a range:

/^(\([0-9]{2}\))\s([0-9]{4,5})-([0-9]{4})$/

Though your regex seems to be fine, as it has an optional 9 to match before the 4 digits. But if there is any possibility that there can be anything else than 9 there, then you can use this regex.

Upvotes: 4

Related Questions