简体   繁体   English

如何使用正则表达式验证多个电话号码

[英]How to validate multiple phone number using regular expression

I have a scenario, where i have to validate multiple phone numbers at a time. 我有一种情况,我必须一次验证多个电话号码。 For example i will input phone number like this in the grid. 例如,我将在网格中输入这样的电话号码。

+46703897733;+46733457773;+46703443832;+42708513544;+91703213815;+919054400407. +46703897733; +46733457773; +46703443832; +42708513544; +91703213815; +919054400407。

Any one help me please? 有人可以帮我吗?

Thanks in advance. 提前致谢。

use below code for +46... numbers. 将以下代码用于+46 ...数字。 other number is simlar 其他数字类似

 string regexPattern = @"^+46[0-9]{9}$";
    Regex r = new Regex(regexPattern);

    foreach(string s in numbers)
    {
        if (r.Match(s).Success)
        {
            Console.WriteLine("Match");
        }
    }
  1. Choose appropriate and suitable regular expression eg from here 选择合适的和合适的正则表达式,例如从这里
  2. Iterate through your number collection and validate them one by one like: 遍历您的号码收集并逐个验证它们,例如:

     Regex rgx = new Regex(yourPattern, RegexOptions.IgnoreCase); foreach(string num in numbers) { if(rgx.Matches(num )) //do something you need } 

    You also can add RegularExpressionValidator to your phone number column cells in grid and pass it your pattern. 您还可以将RegularExpressionValidator添加到网格中的电话号码列单元格,并将其传递给您的模式。 Then button click or any event that causes validation will do it for you. 然后单击按钮或任何导致验证的事件将为您完成。

if + is mandatory in your number than do this in c# 如果您的电话号码中的+号比C#中的号强

        string[] numbers = new string[] { "+46703897733","+46733457773","46733457773"};
         string regexPattern = @"^\+(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d$";
        Regex r = new Regex(regexPattern);

        foreach(string s in numbers)
        {
            if (r.Match(s).Success)
            {
               //"+46703897733","+46733457773" are valid in this case
                Console.WriteLine("Match");
            }
        }

if + is not mandatory you can do this 如果+不是必需的,则可以执行此操作

         string regexPattern = @"^\+?(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d$";
         // all the numbers in the sample above will be considered as valid.

Your regular expression pattern should be then: 正则表达式模式应为:

[+][1-9][0-9]* 

and this is as of you required; 这是您的要求; If you want to limit it, like to: +911234567890, then your exp should be: 如果您想限制它,例如:+911234567890,那么您的exp应该是:

[+][1-9][0-9]{11,11}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM