簡體   English   中英

在 C# 中驗證具有特定字符大小的電話號碼

[英]Validating phone numbers with specific character size in C#

我的程序中有一個方法可以驗證電話號碼是否有 13 個字符,如果字符多於或少於 13 個,我想返回 false,如果有 13 個字符,則返回 true。 到目前為止,我做了這個,我想我得到了 13 個字符,任何更少或更多它都會給我一個 ArgumentOutOfRangeException:

public bool CheckPhone()
        {
            int sum = 0;
            int rest;
            int i;

            for (i = 1; i <= 12; i++)
            {
                if (Phone.Substring(i - 1, i - (i - 1)).Contains("")==false)
                {
                    return false;
                }
                else
                {
                    sum += int.Parse(Phone.Substring(i - 1, i - (i - 1))) * (13 - i);
                }
            }
            rest = (sum * 11) % 12;

            if ((rest == 11) || (rest == 12))
            {
                rest = 0;
                return true;
            }
            else
            {
                return false;
            }
        }
public bool CheckPhone()
    => Phone.Length == 13 && Phone.All(Char.IsDigit);

暫無
暫無

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

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