如何验证(特定国家的)电话号码 [英] How to validate a (country specific) phone number
本文介绍了如何验证(特定国家的)电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有效的电话号码包含:
- 少于9个字符
- "+"号表示一开始
- 仅数字.
我正在尝试使用正则表达式,但是我只是开始使用它们而我并不擅长.到目前为止,我的代码是:
I'm trying to use regular expressions but I've only started using them and I'm not good at it. The code I have so far is:
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();
}
但是我不知道如何以这种方式检查字符串的长度.感谢您的帮助.
But I don't know how to check the length of the string this way. Any help is appreciated.
推荐答案
Jacek 的正则表达式可以正常工作
Jacek's regex works fine
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;
}
}
这篇关于如何验证(特定国家的)电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文