Categories
ASP.NET C#

Ways to validate Phone Numbers in C#

using System;
using System.Text.RegularExpressions;

class RegexSubstitution
{
   public static void Main()
   {
      string text = "124";

      if ( !Regex.Match(text,@"^[1-9]\d{2}-[1-9]\d{2}-\d{4}$" ).Success )
      {
         Console.WriteLine( "Invalid phone number");
      }   
   }
}

Validate Phone Number

public bool IsValidUSPhone(string number)
{
    return Regex.IsMatch(number, @"\(\d{3}\)\s\d{3}-\d{4}");
}

Matching and Grouping Regular Expressions using Regex in C#