Quick ASP.NET form email validation

A combination of a RequiredFieldValidator and a RegularExpressionValidator to validate email addresses on forms:

<asp :TextBox ID="txtEmail" RunAt="Server" />
<asp :RequiredFieldValidator
    ID="reqEmail"
    RunAt="server"
    ControlToValidate="txtEmail"
    ErrorMessage="Email Address required" />
<asp :RegularExpressionValidator
    ID="reqAuthEmail"
    RunAt="server"
    ControlToValidate="txtEmail"
    ValidationExpression="^[\w-\.]+@([\w-]+\.)+[\w-]{2,3}$"
    ErrorMessage="Valid Email Address required" />
  • Share/Bookmark

8 Comments

SpencerNovember 28th, 2006 at 5:48 am

How would you install this and use it? Do you have to have ASP for it? Could you tell me what I would need to use this code for my site? Thanks!

Mikhail EstevesNovember 30th, 2006 at 12:43 am

@Spencer: You can use this on any ASP.NET form.

TimDecember 11th, 2006 at 10:36 am

The code for the regularexpressionvalidator was exactly what I was looking for. Thanks.

AhtisamAugust 3rd, 2007 at 2:15 pm

Thanks Man this realy good ..

nagAugust 16th, 2007 at 6:39 am

Thank you very much for this code

LizJuly 8th, 2008 at 9:44 pm

Thank you very much! Your code works perfectly, I was having a headache trying to make my code function. ;)

GordonMarch 9th, 2010 at 3:06 am

Thanks, This is very helpful

XanderAugust 28th, 2010 at 2:19 am

“Mmm regex hauuughggugh” (that’s my homer impression) Thanks for this, works wonderfully.

Leave a comment

Your comment