代码之家  ›  专栏  ›  技术社区  ›  PrateekSaluja

使用C 2008中的正则表达式验证进行电话号码验证?

  •  3
  • PrateekSaluja  · 技术社区  · 15 年前

    我想验证此格式的电话号码,即+919981424199,+91231456789,… 我正在使用ASP.NET+C_2008开发网站。为此,我使用了正则表达式验证控件->属性->验证表达式=“[0-9]+(,[0-9]+)*”。 但这个号码是919981424199,…… 用户可以这样输入+919981424199930951916,依此类推。 因此,我需要此类型格式的验证表达式。“+”符号是可选的,它可以使用,但不能。我试图解决但仍在搜索。请帮助我解决此问题。 事先谢谢。

    2 回复  |  直到 12 年前
        1
  •  1
  •   Turtle    15 年前

    试试这个: \+?\d+

        2
  •  0
  •   Damian Powell    15 年前

    运行此:

    var match = Regex.Match(phoneNumberTextBox.Text, @"(?<=^|,)\+?\d+");
    while (match.Success)
    {
        string phoneNumber = match.Groups[0].Value;
        Console.WriteLine("Found phone number: " + phoneNumber);
        match = match.NextMatch();
    }
    

    有了这些数据:

    +919981424199,919300951916
    

    生成此输出:

    Found phone number: +919981424199 
    Found phone number: 919300951916 
    
    推荐文章