Regex Phone Validation [duplicate]

0

I'm currently using a regular expression to validate phones in the following format:

  

(xx) xxxxx-xxxx

It accepts only numbers that have this exact format. I would like to know how to modify the expression to accept numbers with either 9 digits (cell phones), either with 8 (fixed), something like:

  

(xx) [x] xxxx-xxxx

The expression follows.

/^\([0-9]{2}\) [0-9]{5}-[0-9]{4}$/
    
asked by anonymous 22.02.2018 / 14:54

1 answer

0

You can use the following Regex:

/^\([0-9]{2}\) [0-9]?[0-9]{4}-[0-9]{4}$/
    
22.02.2018 / 15:06