[英]How to validate multiple phone number using regular expression
我有一种情况,我必须一次验证多个电话号码。 例如,我将在网格中输入这样的电话号码。
+46703897733; +46733457773; +46703443832; +42708513544; +91703213815; +919054400407。
有人可以帮我吗?
提前致谢。
将以下代码用于+46 ...数字。 其他数字类似
string regexPattern = @"^+46[0-9]{9}$";
Regex r = new Regex(regexPattern);
foreach(string s in numbers)
{
if (r.Match(s).Success)
{
Console.WriteLine("Match");
}
}
遍历您的号码收集并逐个验证它们,例如:
Regex rgx = new Regex(yourPattern, RegexOptions.IgnoreCase); foreach(string num in numbers) { if(rgx.Matches(num )) //do something you need }
您还可以将RegularExpressionValidator添加到网格中的电话号码列单元格,并将其传递给您的模式。 然后单击按钮或任何导致验证的事件将为您完成。
如果您的电话号码中的+号比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");
}
}
如果+不是必需的,则可以执行此操作
string regexPattern = @"^\+?(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d$";
// all the numbers in the sample above will be considered as valid.
正则表达式模式应为:
[+][1-9][0-9]*
这是您的要求; 如果您想限制它,例如:+911234567890,那么您的exp应该是:
[+][1-9][0-9]{11,11}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.