【问题标题】:How can I verify a email and a phone number through a single resex?我如何通过一个 resex 验证电子邮件和电话号码?
【发布时间】:2017-03-31 04:00:34
【问题描述】:

好吧,我有 2 个单独的正则表达式分别用于验证电子邮件和电话号码。喜欢

email = "^\w+\@([\da-zA-Z\-]{1,}\.){1,}[\da-zA-Z-]{2,6}" 

phone = "^[2-9]\d{2}-\d{3}-\d{4}"

但是,我想从一个可以是电子邮件和电话的字段中输入输入。我必须验证它们。 我试过使用 ||这两个 resex 或之间的运算符,但没有工作。

[Required]
[MaxLength(50)]
[RegularExpression(@"^\w+\@([\da-zA-Z\-]{1,}\.){1,}[\da-zA-Z-]{2,6} || ^[2-9]\d{2}-\d{3}-\d{4}$", ErrorMessage = "Invalid Phone number Email Address")]
[Display(Name = "Phone number or Email")]        
[System.Web.Mvc.Remote("IsUserExists", "Account", HttpMethod = "POST", ErrorMessage = "This Phone number or Email already exists")]
public string MobOrEmail { get; set; } 

所以我需要一个适当的正则表达式,它可以一次验证电子邮件或电话号码。或者它们之间的 or 操作将是完美的,我非常需要它。谢谢。

【问题讨论】:

  • OR 需要一个 | 字符(不是 2 个)
  • 我尝试过使用单管道 (|),但两者都不起作用,它只适用于电子邮件。
  • @Stephen Muecke 我认为这可以作为答案。
  • @lazycoder 你是在区号后面加空格吗?我注意到它需要一个破折号。
  • 参考Regexr - 工作正常。但附带说明一下,对于电子邮件地址来说,这是一个非常糟糕的正则表达式

标签: regex entity-framework email asp.net-mvc-4 phone-number


【解决方案1】:

您需要一个 |(竖线)字符(不是 2 个)并且没有空格。应该是

[RegularExpression(@"^\w+\@([\da-zA-Z\-]{1,}\.){1,}[\da-zA-Z-]{2,6}|[2-9]\d{2}-\d{3}-\d{4}", ErrorMessage = "...")

参考regexr

请注意,您的电子邮件正则表达式不是很强大。 jquery.validate.js使用的那个是

/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$

为了在模型中更容易阅读和重用,您可以创建自己的属性来扩展 ReqularExpressionAttribute

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class EmailOrPhoneAttribute : RegularExpressionAttribute
{

    public EmailOrPhoneAttribute () : base(@"^\w+\@([\da-zA-Z\-]...etc")
    {
        // Add default error message
        ErrorMessage = "Invalid Phone number Email Address";
    }
}

并在global.asax.cs注册

protected void Application_Start()
{
    ....
    DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(EmailOrPhoneAttribute), typeof(RegularExpressionAttributeAdapter));
    ....
}

然后在模型中

[EmailOrPhone]
public string MobOrEmail { get; set; }

【讨论】:

  • 你能在一个正则表达式中同时结合电话号码和 jQuery 电子邮件吗?
  • 当然,您可以只使用上面的 jquery 正则表达式并将 |[2-9]\d{2}-\d{3}-\d{4} 添加到您的 RegularExpressionAttribute 的末尾(但您将无法使用 [EmailAddress] 属性,因为这会阻止电话号码。
  • 虽然属性很长,但您可能需要考虑从RegularExpressionAttribute 创建一个属性 - 比如[EmailOrPhone(ErrorMessage = "...")] 并注册它global.asax
  • 我是这么想的。因为这个正则表达式太大而无法处理。我更喜欢写Global.asax而不是Mode
  • 是的!我已经完成了。反正。感谢您的支持。
猜你喜欢
  • 2018-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-25
  • 2020-04-05
  • 1970-01-01
  • 2020-08-17
相关资源
最近更新 更多