8
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;
}

This is what I have but I get errors saying Unrecognized escape sequence. Can anbody help? needs to be able to have +44.

Re Captcha
  • 3,125
  • 2
  • 22
  • 34
user3895591
  • 117
  • 1
  • 1
  • 6

5 Answers5

15

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;
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
7

This is a pretty solid regex, will handle area codes, extension numbers and the +44 international code as well as mobile numbers and even 10 digit numbers:

^(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?|\#)\d{3,4})?$
RattyLaa
  • 323
  • 3
  • 12
  • 3
    This seems a pretty comprehensive regex pattern that matches all valid UK telephone formats listed here: [area-codes.org.uk](http://www.area-codes.org.uk/formatting.php) My tests can be found here: [regex101.com](https://regex101.com/r/f0HaLr/3) – AndySX Sep 22 '17 at 10:33
2

Try using this:

^(\+44\\s?7\\d{3}|\(?07\\d{3}\)?)\\s?\\d{3}\\s?\\d{3}$

In order for the regex to recognize the \s, \d, etc you need to put double slash \\. If not you'll get an illegal escape character error.

1

This works well and allows 3 and 4 digit extensions. It also ensures that only mobile numbers are entered. UK mobile numbers start with 07 regardless of the provider

^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}(\s?\#(\d{4}|\d{3}))?$
Francis Benyah
  • 567
  • 7
  • 11
0

Try this code,

    using System.Text.RegularExpressions; 
    public static bool CheckNumber(string strPhoneNumber)
    {
            string MatchNumberPattern = "^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$";
            if (strPhoneNumber != null)
            {
                return Regex.IsMatch(strPhoneNumber, MatchNumberPattern);
            }
            else
            {
                return false;
            }
     }

Hope, this code helps you.

Nitika Chopra
  • 1,281
  • 17
  • 22