驗證郵箱地址和手機號碼 .

 
  1. namespace test  
  2. {  
  3.     class Test5  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             Console.Write("請輸入要驗證的手機號碼:");  
  8.             string telephone = Console.ReadLine();  
  9.             bool result1 = CheckPhone(telephone);  
  10.             if (result1 == true)  
  11.             {  
  12.                 Console.WriteLine("手機號碼{0}合法!", telephone);  
  13.             }  
  14.             else  
  15.             {  
  16.                 Console.WriteLine("手機號碼{0}不合法!", telephone);  
  17.             }  
  18.   
  19.             Console.Write("請輸入要驗證的郵箱地址:");  
  20.             string email = Console.ReadLine();  
  21.             bool result2 = CheckEmail(email);  
  22.             if (result2 == true)  
  23.             {  
  24.                 Console.WriteLine("郵箱地址{0}合法!", email);  
  25.             }  
  26.             else  
  27.             {  
  28.                 Console.WriteLine("郵箱地址{0}不合法!", email);  
  29.             }  
  30.         }  
  31.   
  32.         /// <summary>   
  33.         /// 驗證手機號碼的有效性   
  34.         /// </summary>   
  35.         /// <param name="phone">待驗證的手機號碼</param>   
  36.         /// <returns>true合法,false不合法</returns>   
  37.         static bool CheckPhone(string phone)  
  38.         {  
  39.             if (phone.Length != 11)  
  40.             {  
  41.                 return false;  
  42.             }  
  43.             else  
  44.             {  
  45.                 //驗證是否由數字構成   
  46.                 char[] chs = phone.ToCharArray();  
  47.                 for (int index = 0; index < chs.Length; index++)  
  48.                 {  
  49.                     if (chs[index] < '0' || chs[index] > '9')  
  50.                     {  
  51.                         return false;  
  52.                     }  
  53.                 }  
  54.                 return true;  
  55.             }  
  56.         }  
  57.   
  58.         /// <summary>   
  59.         /// 驗證郵箱地址的有效性(必須包含@和.並且以.com或.cn結尾)   
  60.         /// </summary>   
  61.         /// <param name="email">待驗證的郵箱地址</param>   
  62.         /// <returns>true合法,false不合法</returns>   
  63.         static bool CheckEmail(string email)  
  64.         {  
  65.             //判斷是否包含@和.符號   
  66.             if (email.IndexOf("@") == -1 || email.IndexOf(".") == -1)  
  67.             {  
  68.                 return false;  
  69.             }  
  70.             else  
  71.             {  
  72.                 //判斷是否以.com或.cn結尾   
  73.                 if (email.EndsWith(".com") == true || email.EndsWith(".cn") == true)  
  74.                 {  
  75.                     return true;  
  76.                 }  
  77.                 else  
  78.                 {  
  79.                     return false;  
  80.                 }  
  81.             }  
  82.         }  
  83.     }  
  84. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章