When entering a phone number, the following format must be followed:
- The phone number may start with a "1," which is optional.
- There can be a space after the starting digit, which is also optional.
- An opening parenthesis "(" is optional.
- This should be followed by 3 digits and then a closing parenthesis ")"
- After that, there can be spaces, no space at all, or a hyphen "-"
- Another optional opening parenthesis "("
- Another set of 3 digits followed by a closing parenthesis ")"
- Then another round of options for space, no space, or a hyphen "-"
- Finally, one more optional opening parenthesis "("
- Followed by 3 digits
Examples: 5555555555
, 555 555 5555
, 555-555-5555
, (555)-(555)-5555
Examples cont.: 1 555 555 5555
, 1555 555 5555
, 1555-555-5555
, 1-555-555-5555
, 1 (555) (555) 5555
/^([1]{0,1})\s?\(?\d{3}\)?[-\s]?\d{3}[-\s]?\d{4}$/
The regex above works on most cases but fails if an opening parentheses is used without a closing parentheses like in the examples below. If this issue could be fixed, it is appreciated.Please help!
Additional examples: 1 (555-555-5555
and 555) 555 5555