How to validate a (country specific) phone number

前端 未结 8 676
一个人的身影
一个人的身影 2020-12-30 00:44

A valid phone number contains:

  • Less than 9 characters
  • A "+" at the start
  • Only digits.

I\'m trying to use regular expre

8条回答
  •  情歌与酒
    2020-12-30 00:52

    Jacek's regex works fine

    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("Enter a phone number.");
            string telNo = Console.ReadLine();                      
            Console.WriteLine("{0}correctly entered", IsPhoneNumber(telNo) ? "" : "in");    
            Console.ReadLine(); 
        }
    
        public static bool IsPhoneNumber(string number)
        {
            return Regex.Match(number, @"^(\+[0-9]{9})$").Success;
        }
    }
    

提交回复
热议问题