簡體   English   中英

如何在C#上檢查電話號碼?

[英]How to check phone numbers on c#?

我的目標是查看用戶是否輸入一次有效的電話號碼。 如果沒有,我只是告訴他們錯了。 我看過如何在C#和其他代碼中將字符串格式化為電話號碼 ,但是我仍然不確定。

我需要做的是確保用戶輸入一個有效的數字,意思是1)只能是數字2)破折號必須在正確的位置,並且3)其正確的字符數。

我做了第三個。 第1個應該只是一個TryParse驗證。.我主要是在弄清楚第2個。

string phone = i["MyPhone"].ToString();
string area = phone.Substring(0, 3);
string major = phone.Substring(3, 3);
string minor = phone.Substring(6);
string formatted = string.Format("{0}-{1}-{2}", area, major, minor);

現在,顯示.Format。 我認為我不需要這樣做,因為我只能驗證數字的格式是否正確,而不是將數字實際放在短划線之間。

因此,最終結果將是請輸入數字:123-456-7890恭喜,此數字有效

要么

請輸入數字:123-45-45-4這個數字無效。

避免使用正則表達式解決方案,並且數量不一定是真實的。 只要符合上述格式即可。

   bool wrong = true;

        while (wrong)
        {
            string phoneNumber = Console.ReadLine();
            string[] values = phoneNumber.Split('-');
            while (string.IsNullOrWhiteSpace(phoneNumber))
            {
                Console.WriteLine("Invalid - Please do not leave blank");
            }

            if (values.Length == 3 && values[0].Length == 3 && values[1].Length == 3 && values[2].Length == 4)
            {
                int yesnumber;
                List<int> intvalues = new List<int>();
                for (int number = 0; number < values.Length; number++)
                {
                    bool isNumeric = Int32.TryParse(values[number], out yesnumber);
                    if (isNumeric == true)
                    { intvalues.Add(yesnumber); }
                }
                if(intvalues.Count == 3)
                {
                    Console.WriteLine("Congratulations This is a valid number");
                    break;
                }

                else { Console.WriteLine("This is not a valid number"); }
            }

            else { Console.WriteLine("This is not a valid number"); }
        }
        Console.ReadLine();
    }

這是一個簡單的控制台應用程序,但是它非常適合要求用戶以該特定格式擁有您要求的所有內容。

編輯*我修復了不檢查數字的錯誤。

即使您不使用正則表達式引擎,也可以使用模式。 例如,一個數字是有效的“如果它的數字與模式位於相同的位置,並且非數字完全相同”,可以這樣編碼:

bool IsPhoneNumber(string input, string pattern)
{
     if (input.Length != pattern.Length) return false;

     for( int i = 0; i < input.Length; ++i ) {
         bool ith_character_ok;
         if (Char.IsDigit(pattern, i))
             ith_character_ok = Char.IsDigit(input, i);
         else
             ith_character_ok = (input[i] == pattern[i]);

         if (!ith_character_ok) return false;
     }
     return true;
}

現在,您甚至可以檢查多種模式,例如

if (IsPhoneNumber(input, "000-000-0000") || IsPhoneNumber(input, "(000) 000-0000"))

但是它也可以用於匹配其他內容,例如,社會保險號碼的格式為000-00-0000

List<string> phoneNumbers = new List<string> {"111-111-1111",
                                             "1-111-111-1111",
                                             "111111-1111",
                                             "111-1111111",
                                             "(111)111-1111",
                                             "(111) 111-1111",
                                             "11111111111",
                                             "111111-11111",
                                             "1111111111111"};

foreach(string phoneNumber in phoneNumbers)
{
    Match match = Regex.Match(phoneNumber,@"^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$", RegexOptions.IgnoreCase);

    Console.WriteLine(phoneNumber + " " + match.Success);
}


//111-111-1111   True
//1-111-111-1111 True
//111111-1111    True
//111-1111111    True
//(111)111-1111  True
//(111) 111-1111 True
//11111111111    True
//111111-11111   False
//1111111111111  True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM