- namespace test
- {
- class Test5
- {
- static void Main(string[] args)
- {
- Console.Write("請輸入要驗證的手機號碼:");
- string telephone = Console.ReadLine();
- bool result1 = CheckPhone(telephone);
- if (result1 == true)
- {
- Console.WriteLine("手機號碼{0}合法!", telephone);
- }
- else
- {
- Console.WriteLine("手機號碼{0}不合法!", telephone);
- }
- Console.Write("請輸入要驗證的郵箱地址:");
- string email = Console.ReadLine();
- bool result2 = CheckEmail(email);
- if (result2 == true)
- {
- Console.WriteLine("郵箱地址{0}合法!", email);
- }
- else
- {
- Console.WriteLine("郵箱地址{0}不合法!", email);
- }
- }
- /// <summary>
- /// 驗證手機號碼的有效性
- /// </summary>
- /// <param name="phone">待驗證的手機號碼</param>
- /// <returns>true合法,false不合法</returns>
- static bool CheckPhone(string phone)
- {
- if (phone.Length != 11)
- {
- return false;
- }
- else
- {
- //驗證是否由數字構成
- char[] chs = phone.ToCharArray();
- for (int index = 0; index < chs.Length; index++)
- {
- if (chs[index] < '0' || chs[index] > '9')
- {
- return false;
- }
- }
- return true;
- }
- }
- /// <summary>
- /// 驗證郵箱地址的有效性(必須包含@和.並且以.com或.cn結尾)
- /// </summary>
- /// <param name="email">待驗證的郵箱地址</param>
- /// <returns>true合法,false不合法</returns>
- static bool CheckEmail(string email)
- {
- //判斷是否包含@和.符號
- if (email.IndexOf("@") == -1 || email.IndexOf(".") == -1)
- {
- return false;
- }
- else
- {
- //判斷是否以.com或.cn結尾
- if (email.EndsWith(".com") == true || email.EndsWith(".cn") == true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
- }
驗證郵箱地址和手機號碼 .
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.