Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email Regular Expression Validation

Can anyone correct the expression below to also not allow blank field?

<asp:RegularExpressionValidator ID="expEmail" runat="server" ControlToValidate="txtEmail" ErrorMessage="valid email address required" ValidationExpression="^([a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]){1,70}$"></asp:RegularExpressionValidator>
like image 829
LiamB Avatar asked Apr 16 '10 11:04

LiamB


2 Answers

One of the solutions (not a best one) is to implement some regular expression:

^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$

Sample: [email protected]

But actually, it's very hard to write and maintain a good email validation regexp. You shouldn't trust 100% your regex. You can find several articles about that difficulty on the Internet:

  • It's Impossible to Validate an Email Address (elliot.land) - discussion on Hacker News
  • Stop Validating Email Addresses With Regex
  • Email Address Regular Expression That 99.99% Works.
like image 195
akrisanov Avatar answered Oct 10 '22 04:10

akrisanov


Add a required field validator as well - I think the regex validator will only fire if there is text in the field to look at.

like image 25
Paddy Avatar answered Oct 10 '22 04:10

Paddy