如何验证电话号码

Ada*_*ins 15 c# regex

有效的电话号码包含:

少于9个字符,开头是"+",只有数字.

我试图使用正则表达式,但我只是开始使用它们,我不擅长它.我到目前为止的代码是:

static void Main(string[] args)
{
    Console.WriteLine("Enter a phone number.");
    string telNo = Console.ReadLine();

    if (Regex.Match(telNo, @"^(\+[0-9])$").Success)
        Console.WriteLine("correctly entered");

    else
        Console.WriteLine("incorrectly entered");

    Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

但我不知道如何以这种方式检查字符串的长度.任何帮助表示赞赏.

Gre*_*reg 20

Jacek的正则表达式很好

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Enter a phone number.");
        string telNo = Console.ReadLine();                      
        Console.WriteLine("{0}correctly entered", IsPhoneNumber(telNo) ? "" : "in");    
        Console.ReadLine(); 
    }

    public static bool IsPhoneNumber(string number)
    {
        return Regex.Match(number, @"^(\+[0-9]{9})$").Success;
    }
}
Run Code Online (Sandbox Code Playgroud)


Jac*_*cek 17

你的正则表达式应该是这样的,你需要有关char计数器的信息

@"^(\+[0-9]{9})$"
Run Code Online (Sandbox Code Playgroud)

  • @Adam Higgins在那里https://regex101.com/找到更多帮助.这个正则表达式看起来不错 (2认同)

use*_*704 14

不要使用正则表达式!!

正则表达式的变量太多,没有任何用处。相反,只需从字符串中删除所有非 0-9 的字符,然后检查是否剩余正确的位数。那么用户包含或不包含哪些额外内容并不重要... ()x-+[] 等,因为它只是将它们全部删除并且只计算字符 0-9。

我有一个很好用的字符串扩展,并且支持多种格式。它接受一个IsRequired参数。因此,您可以像这样验证电话号码:

string phone = "(999)999-9999"
bool isValidPhone = phone.ValidatePhoneNumber(true) // returns true

string phone ="1234567890"
bool isValidPhone = phone.ValidatePhoneNumber(true) // returns true

string phone = ""
bool isValidPhone = phone.ValidatePhoneNumber(false) // not required, so returns true

string phone = ""
bool isValidPhone = phone.ValidatePhoneNumber(true) // required, so returns false

string phone ="12345"
bool isValidPhone = phone.ValidatePhoneNumber(true) // returns false

string phone ="foobar"
bool isValidPhone = phone.ValidatePhoneNumber(true) // returns false
Run Code Online (Sandbox Code Playgroud)

这是代码(假设是 10 位美国电话号码。相应地调整):

public static class StringExtensions
{

    /// <summary>
    /// Checks to be sure a phone number contains 10 digits as per American phone numbers.  
    /// If 'IsRequired' is true, then an empty string will return False. 
    /// If 'IsRequired' is false, then an empty string will return True.
    /// </summary>
    /// <param name="phone"></param>
    /// <param name="IsRequired"></param>
    /// <returns></returns>
    public static bool ValidatePhoneNumber(this string phone, bool IsRequired)
    {
        if (string.IsNullOrEmpty(phone) & !IsRequired)
            return true;

        if (string.IsNullOrEmpty(phone) & IsRequired)
            return false;

        var cleaned = phone.RemoveNonNumeric();
        if (IsRequired)
        {
            if (cleaned.Length == 10)
                return true;
            else
                return false;
        }
        else
        {
            if (cleaned.Length == 0)
                return true;
            else if (cleaned.Length > 0 & cleaned.Length < 10)
                return false;
            else if (cleaned.Length == 10)
                return true;
            else
                return false; // should never get here
        }
    }

    /// <summary>
    /// Removes all non numeric characters from a string
    /// </summary>
    /// <param name="phone"></param>
    /// <returns></returns>
    public static string RemoveNonNumeric(this string phone)
    {
        return Regex.Replace(phone, @"[^0-9]+", "");
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意:此方法仅适用于美国电话号码。在我的国家(波兰),我们有 9 位数的手机号码。此外,人们有时会添加国家/地区代码(波兰为+48),因为没有它,号码将无法在其他欧盟国家/地区使用(好吧,如果有人在不同国家/地区拥有此号码,则可能会使用:D)。 (2认同)