正則表示式 驗證輸入為電話號碼或手機
阿新 • • 發佈:2019-01-30
using System; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static void Main() { var regexTelephone = new Regex(@"^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$"); var regexMobilePhone = new Regex(@"^[1][358]\d{9}$"); while (true) { Console.Write("Phone:"); var inputString = Console.ReadLine(); if (string.IsNullOrWhiteSpace(inputString)) continue; if (regexTelephone.IsMatch(inputString) || regexMobilePhone.IsMatch(inputString)) { Console.WriteLine("Okay!"); break; } Console.WriteLine("Wrong!"); } Console.Write("Press any key to exit..."); Console.ReadKey(); } } }