印度电话号码的正则表达式 [英] regular expression for indian phone number
问题描述
我正在寻找印度电话号码的正则表达式
am looking for the regular expression of indian phone number
正则表达式应允许以下所有格式.
the regular expression should allow all the following formats.
座机号码
0802404408
080-2404408
+91802404408
+91-802404408
对于手机号码
8147708287
08147708287
+918147708287
+91-8147708287
任何人都可以帮助我,谢谢!
can anyone help me, thanks in advance
我的代码是
[RegexValidator("[0-9 -]*"
, MessageTemplateResourceName = "INVALID_PHONE"
, MessageTemplateResourceType = typeof(ValidatioinErrors))]
public string Phone
{
get { return phone; }
set { phone = value; }
}
public bool IsValid()
{
return Validation.Validate<Class_name>(this).IsValid;
}
public ValidationResults ValResults
{
get
{
return Validation.Validate<Class_name>(this);
}
}
对于我刚刚提到的这个验证问题
for this validation thing I just referred
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
在我的命名空间中,在UI部分中,表达式可以正常工作,但是在上面的代码中,如果我将值设置为080-2404408,则显示无效的电话号码"
in my namespace, in the UI part the expression is working fine, but in the code behind as above, it shows "Invalid Phone number", if I give value as 080-2404408
推荐答案
您可以尝试
^\+?[0-9-]+$
重要的部分是用于字符串开头和结尾的锚点^
和$
.我还在开头添加了\+?
,以匹配可选的+
. +
需要转义,因为它是正则表达式中的特殊字符,?
使其成为可选字符之后.
The important parts are the anchors ^
and $
for the start and the end of the string. I added also \+?
at the start, to match an optional +
. The +
needs to be escaped since it is a special character in regex, the ?
after it makes it optional.
当然,这是一个非常简单的模式,请注意,例如"-----"也将有效.
Of course this is a very simple pattern, be aware that e.g. "-----" would also be valid.
这篇关于印度电话号码的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!