A valid phone number contains:
I'm trying to use regular expressions but I've only started using them and I'm not good at it. The code I have so far is:
static void Main(string[] args) { Console.WriteLine("Enter a phone number."); string telNo = Console.ReadLine(); if (Regex.Match(telNo, @"^(\+[0-9])$").Success) Console.WriteLine("correctly entered"); else Console.WriteLine("incorrectly entered"); Console.ReadLine(); }
But I don't know how to check the length of the string this way. Any help is appreciated.
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; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With