[英]Email Address Validation with RegularExpression Annotation Parse Error
In my attempt to use the RegularExpression annotation to validate an e-mail address I keep getting parse errors in reverse the moment the create page loads. 在尝试使用RegularExpression批注来验证电子邮件地址时,在创建页面加载后,我不断收到相反的解析错误。 To get the included quotes as part of the regular expression I tried using the unicode so that it would work in Visual Studio. 为了将包含的引号作为正则表达式的一部分,我尝试使用unicode,以便它可以在Visual Studio中使用。 That didn't work either. 那也不起作用。 I tried two quotes as well. 我也尝试了两个引号。
This is the original regex I wanted to use: 这是我想使用的原始正则表达式:
(?:[a-z0-9!#$%&' +/=?^_
{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_
{|}~-]+) |"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f]) ")@(?:(?:a-z0-9?.)+a-z0-9?|[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-] [a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)]) (?:[a-z0-9!#$%&' + / =?^ _{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_
{|} 〜- ] +) |“(?:[\\ x01- \\ x08 \\ x0b \\ x0c \\ x0e- \\ x1f \\ x21 \\ x23- \\ x5b \\ x5d- \\ x7f] | \\ [\\ x01- \\ x09 \\ x0b \\ x0c \\ x0e- \\ x7f]) “)@(?:( ?: a-z0-9?。)+ a-z0-9?| [(?:( ?: 25 [0-5] | 2 [0-4] [0-9] | [01]?[0-9] [0-9]?)。){3}(?: 25 [0-5] | 2 [0-4] [0-9] | [01]?[0-9] [0-9]?| [a-z0-9-] [a-z0-9]:(?:[\\ x01- \\ x08 \\ x0b \\ x0c \\ x0e- \\ x1f \\ x21- \\ x5a \\ x53- \\ x7f] | \\ [\\ x01- \\ x09 \\ x0b \\ x0c \\ x0e- \\ x7f])+)]]))
This is the unicode I replaced some of the included quotes with: 这是我用一些引号替换的unicode:
" \\ u0022
[Required(ErrorMessage = "User e-mail required.")]
[Display(Name = "E-mail")]
[RegularExpression(@"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\u0022(?:[\x01 -\x08\x0b\x0c\x0e -\x1f\x21\x23 -\x5b\x5d -\x7f] |\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\u0022)@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])", ErrorMessage = "Invalid format.")]
public string email { get; set; }
How should I be doing this really? 我真的应该怎么做?
You can go for following code:
[Required(ErrorMessage = "User e-mail required.")]
[Display(Name = "E-mail")]
[EmailAddress(ErrorMessage = "Invalid format.")]
public string email { get; set; }
OR
[Required(ErrorMessage = "User e-mail required.")]
[Display(Name = "E-mail")]
[RegularExpression(@"/^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,4}$/",ErrorMessage = "Invalid format.")]
public string email { get; set; }
REGEXP REGEXP
^[A-Za-z0-9\x{0430}-\x{044F}\x{0410}-\x{042F}\._-]+@(?:[A-Za-z0-9\x{0430}-\x{044F}\x{0410}-\x{042F}]{1,2}|[A-Za-z0-9\x{0430}-\x{044F}\x{0410}-\x{042F}](?:(?<!(\.\.))[A-Za-z0-9\x{0430}-\x{044F}\x{0410}-\x{042F}.-])+[A-Za-z0-9\x{0430}-\x{044F}\x{0410}-\x{042F}])\.[A-Za-z\x{0430}-\x{044F}\x{0410}-\x{042F}]{2,}$
RESULT 结果
potato@potato.com <== True
test@test.com <== True
test@test@test.com <== False
test@test <= False
See: https://regex101.com/r/yL5M5m/1 参见: https : //regex101.com/r/yL5M5m/1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.