I know there are a ton of regex examples on how to match certain phone number types. For my example I just want to allow numbers and a few special characters. I am again having trouble achieving this.
Phone numbers that should be allowed could take these forms
5555555555
555-555-5555
(555)5555555
(555)-555-5555
(555)555-5555 and so on
I just want something that will allow [0-9] and also special characters '(' , ')', and '-'
so far my expression looks like this
/^[0-9]*^[()-]*$/
I know this is wrong but logically I believe this means allow numbers 0-9 or and allow characters (, ), and -.
^(\(\d{3}\)|\d{3})-?\d{3}-?\d{4}$
\(\d{3}\)|\d{3}
three digits with or without ()
- The simpler regex would be \(?\d{3}\)?
but that would allow (555-5555555
and 555)5555555
etc.-
followed by three digits-
followed by four digitsNote that this would still allow 555555-5555
and 555-5555555
- I don't know if these are covered in your and so on part
This match what you want numbers,(, ) and -
/^[0-9()-]+$/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With