0

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. To get the included quotes as part of the regular expression I tried using the unicode so that it would work in 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])+)])

This is the unicode I replaced some of the included quotes with:

\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?

Nathan McKaskle
  • 2,926
  • 12
  • 55
  • 93
  • 1
    With the `EmailAddressAttribute`? It's as easy as `[EmailAddress]`. IMO regex and email are poor bedfellows. – spender Jan 13 '17 at 17:50
  • EmailAddressAttribute. http://stackoverflow.com/questions/16712043/email-address-validation-using-asp-net-mvc-data-type-attributes – Sunny Jan 13 '17 at 17:50
  • OMG I tried this already and it did not work. I was told I needed to use RegularExpression on that question and the question got marked down twice for not including some logic that it apparently needs. I don't know how to use that annotation properly. Is there not something more to it? It didn't work. – Nathan McKaskle Jan 13 '17 at 17:59

2 Answers2

1
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; }
manika
  • 187
  • 2
  • 13
0

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