问题
I need regex which validates UAE mobile phone number like
+9710501234566 or
+971 (050) (123 4566)
Currently I am using
^(\+971[\s]{0,1}[\-]{0,1}[\s]{0,1}|[\s]{0,1}0)(5[056]{1})[\s]{0,1}[\-]{0,1}[\s]{0,1}[1-9]{1}[0-9]{6}$
Could you help me please? I'm not really good with regular expressions...
回答1:
Try this:
Regex regex = new Regex(@"^\+971(\d{10}|\s\(\d{3}\)\s\(\d{3}\s\d{4}\))$");
Basically, you check for +971 as is, then either for 10 digits right after that, or the pattern space - left parenthesis - 3 digits - right parenthesis - space - left parenthesis - 3 digits - space - 4 digits - right parenthesis.
Note that since you mentioned the sample strings to be the only 2 patterns possible, I have used the literal characters '+','(' and ')' in my regex.
If you want to extend this to allow parentheses without space, just replace \s
with \s?
to make spaces optional.
回答2:
+971 (050) (123 4566) is invalid format it should be +971 (50) (123 4566)
this will allow only number starting with 50 (etisalat) 52 (du) 55 (du) 56 (etisalat)
/^5(0|2|5|6)\d{7}$/;
start with 5 and then matches either 0,2,5,6 and then 7 digits numbers below also should work starting with 9715 format
/^+9715(0|2|5|6)\d{7}$/;
来源:https://stackoverflow.com/questions/24646890/regular-expression-for-validating-uae-numbers