如何通过单个 resex 验证电子邮件和 phone 号码?
How can I verify a email and a phone number through a single resex?
好吧,我有 2 个单独的正则表达式用于验证电子邮件和 phone 号码。喜欢
email = "^\w+\@([\da-zA-Z\-]{1,}\.){1,}[\da-zA-Z-]{2,6}"
和
phone = "^[2-9]\d{2}-\d{3}-\d{4}"
但是,我想输入一个字段,可以是电子邮件和 Phone。我必须验证它们。
我试过使用 ||这两个 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; }
所以我需要一个合适的正则表达式,它可以同时验证电子邮件或 phone 号码。或者它们之间的 or 操作将是完美的,我非常需要它。谢谢
您需要一个 |
(竖线)字符(不是 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; }
好吧,我有 2 个单独的正则表达式用于验证电子邮件和 phone 号码。喜欢
email = "^\w+\@([\da-zA-Z\-]{1,}\.){1,}[\da-zA-Z-]{2,6}"
和
phone = "^[2-9]\d{2}-\d{3}-\d{4}"
但是,我想输入一个字段,可以是电子邮件和 Phone。我必须验证它们。 我试过使用 ||这两个 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; }
所以我需要一个合适的正则表达式,它可以同时验证电子邮件或 phone 号码。或者它们之间的 or 操作将是完美的,我非常需要它。谢谢
您需要一个 |
(竖线)字符(不是 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; }