I am using the following REGEX for validating phone numbers
/^(?!([^-]*-){5})(\+\d+)?\s*(\(\d+\))?[- \d]+$/gi
If the input is suppose +51 (0) 989009890 it is valid. But when the input is 0051 (0) 989009890. I am new to REGEX so couldnot find a possible solution.
Phone numbers are valid if it is of the following formats.
- 0xxxxxxxxxxx
- +xx xxxxxxxxxxx
- +xxxxxxxxxxxxx
- +xx (x) xxxxxxxxxx
- 00xxxxxxxxxx
- 00xx (x) xxxxxxxxx
here x denotes numbers 0-9. numbers inside () has limit upto 3.
If you mean to accept
0051 (0) 989009890as well, the problem is (+\d+)? is meant to handle the bit before the parentheses in the input, but it requires a ‘+’ to be present. You could change that by making it optional with a ‘?’, like:Or, if ’00xxx’ should be an alternative to ‘+xxx’ (that is, either a ‘+’ or ’00’ must be present there), then you could use: