Validating UK phone number (Regex C#)

后端 未结 5 865
既然无缘
既然无缘 2021-02-06 05:18
public static bool ValidatePhoneNumber(string number)
{
    return Regex.Match(number, \"^(\\+44\\s?7\\d{3}|\\(?07\\d{3}\\)?)\\s?\\d{3}\\s?\\d{3}$\", RegexOptions.Ignore         


        
5条回答
  •  隐瞒了意图╮
    2021-02-06 05:54

    You may try this regex if you are trying to get it with +44

    ^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$
    

    This will match for

    +447222555555 | +44 7222 555 555 | (0722) 5555555 #2222
    

    REGEX DEMO


    You can try this regex for UK phone numbers:

    /^\(?0( *\d\)?){9,10}$/
    

    This regex will check for 10 or 11 digit numbers which are there in UK numbers, starting with a 0, which may have formatting spaces between any of the digits, and optionally a set of brackets for the area code.

    Also in your regex you need to add @ to get rid of that error(Unrecognized escape sequence):

    public static bool ValidatePhoneNumber(string number)
    {
       return Regex.Match(number, @"^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$", RegexOptions.IgnoreCase).Success;
    }
    

提交回复
热议问题