regular expression for indian phone number

2019-09-21 13:50发布

am looking for the regular expression of indian phone number

the regular expression should allow all the following formats.

for landline no

0802404408
080-2404408
+91802404408
+91-802404408

for mobile no

8147708287
08147708287
+918147708287
+91-8147708287

can anyone help me, thanks in advance

my code is

[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;

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

3条回答
男人必须洒脱
2楼-- · 2019-09-21 14:21

You can use other site for your reference like http://www.jslab.dk/tools.regex.php to generate regular expression for your requirement or download a trial version of this software RegexMagic

Regular Expression generator

查看更多
聊天终结者
3楼-- · 2019-09-21 14:34

You can try

^\+?[0-9-]+$

See it here on Regexr

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.

查看更多
男人必须洒脱
4楼-- · 2019-09-21 14:35

For the examples provided following RegEx works:

/^(?:\+91(?:-)?|0\d{2,4}-|0|)\d{7,10}$/
查看更多
登录 后发表回答