Regular expression for validating UAE numbers

只谈情不闲聊 提交于 2020-01-16 18:40:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!