44

Does anyone know what the regex used by the email validator in ASP.NET is?

halfer
  • 19,824
  • 17
  • 99
  • 186
JamesBrownIsDead
  • 629
  • 1
  • 7
  • 7
  • I don't think there is a built-in "EmailValidator" control you can use. You can roll your own using the RegularExpressionValidator. – Dave Baghdanov Nov 10 '09 at 19:31
  • Not an answer to the question - but here is the simple validation that I use. `.+@.+\..+`. I stick to this one because many emails does not follow standards still they are valid. – LCJ Dec 19 '12 at 13:53

6 Answers6

102

Here is the regex for the Internet Email Address using the RegularExpressionValidator in .NET

\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

By the way if you put a RegularExpressionValidator on the page and go to the design view there is a ValidationExpression field that you can use to choose from a list of expressions provided by .NET. Once you choose the expression you want there is a Validation expression: textbox that holds the regex used for the validator

Josh Mein
  • 28,107
  • 15
  • 76
  • 87
  • 2
    Is that how it's done in ASP.NET's email-validator? That'd match `_@_._`... I'd expected something more fancy. – Bart Kiers Nov 10 '09 at 19:30
  • @Bart check it out for yourself. I told you how I found it – Josh Mein Nov 10 '09 at 19:31
  • I posted my comment when you only posted your regex and nothing more, so no, you didn't tell me how you found it. Besides, now that I read the rest of your post, I (being someone without any .NET experience and no means of testing it) am not able to go to the documentation you might be trying to point to. No matter, I was just a bit surprised that such a naive regex is used. I have no interest in checking it out myself. – Bart Kiers Nov 10 '09 at 19:37
  • 6
    Besides, validating e-mail addresses is just a waste of time, IMO. – Bart Kiers Nov 10 '09 at 19:39
  • 12
    Validating email addresses on the form is never a waste of time. You can still use it in conjunction with other checks. – IrishChieftain Nov 10 '09 at 19:54
  • Thanks for telling me how to find it! – JamesBrownIsDead Nov 10 '09 at 19:55
  • 1
    Well IrishChieftain, it seems we have a different opinion. Note the 'IMO' after my statement. Something that should also belong after yours... IMO. :) – Bart Kiers Nov 10 '09 at 19:59
  • 6
    @BartKiers this is exactly how it should be done. The proper way to validate e-mail is to look for a @ and a dot after it with something between those. A good article on the subject - http://jacobsantos.com/2007/general/stop-doing-email-validation-the-wrong-way/ – Stilgar Mar 26 '12 at 16:42
  • 4
    I think the common misconception is that client side email validation is meant to prevent malicious users from entering in junk emails - It's not. The purpose is simply to assist the user in entering in an email. I don't know how many times I typed my email so fast I forgot the dot in-between the domain and the TLD. – The Muffin Man Jan 16 '14 at 21:49
  • should u check if there is special characters before @ ? http://stackoverflow.com/questions/2049502/what-characters-are-allowed-in-email-address – pata Dec 16 '15 at 20:16
  • @GuillermoVarini This question asked what regex was used by the email validator in ASP.NET. The regex above is not necessarily the best regular expression for email address; it was only the one used by the validator at the time. – Josh Mein Dec 16 '15 at 20:19
  • Just hit a support issue where an old form wasn't accepting .com.au addresses. This appears to be where my predecessor copy/pasted it from. A Planet America bug. – SteveCav Nov 08 '19 at 03:35
24

I don't validate email address format anymore (Ok I check to make sure there is an at sign and a period after that). The reason for this is what says the correctly formatted address is even their email? You should be sending them an email and asking them to click a link or verify a code. This is the only real way to validate an email address is valid and that a person is actually able to recieve email.

Bob
  • 97,670
  • 29
  • 122
  • 130
  • 19
    Email address validation is not meant to prevent the user from entering an address that doesn't belong to them. It's purpose is to prevent users from mistyping an address by accident. So basically what you're saying is you can't fully prevent people from breaking into your house so rather than locking the door, just keeping it closed is good enough. – The Muffin Man Jun 30 '12 at 20:41
  • You could possibly provide a sample for the simple validation with `@` and `.` ( or refer http://stackoverflow.com/questions/742451/what-is-the-simplest-regular-expression-to-validate-emails-to-not-accept-them-bl) – LCJ Dec 19 '12 at 13:55
  • 2
    You need both. You shouldn't have waist your time sending an email out until you at-least know it's a valid email. – capdragon Apr 30 '13 at 15:55
18

E-mail addresses are very difficult to verify correctly with a mere regex. Here is a pretty scary regex that supposedly implements RFC822, chapter 6, the specification of valid e-mail addresses.

Not really an answer, but maybe related to what you're trying to accomplish.

Thomas
  • 174,939
  • 50
  • 355
  • 478
17

We can use RegularExpressionValidator to validate email address format. You need to specify the regular expression in ValidationExpression property of RegularExpressionValidator. So it will look like

 <asp:RegularExpressionValidator ID="validateEmail"    
  runat="server" ErrorMessage="Invalid email."
  ControlToValidate="txtEmail" 
  ValidationExpression="^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$" />

Also in event handler of button or link you need to check !Page.IsValid. Check sample code here : sample code

Also if you don't want to use RegularExpressionValidator you can write simple validate method and in that method usinf RegEx class of System.Text.RegularExpressions namespace.

Check example:

example

Hasan Fathi
  • 5,610
  • 4
  • 42
  • 60
Avinash
  • 199
  • 1
  • 2
  • -1 for providing a solution that incorrectly identifies valid emails as bad. Compare your regex to this: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html You should add a warning that this solution is too simplistic. – oligofren Nov 17 '15 at 13:03
4

For regex, I first look at this web site: RegExLib.com

joerage
  • 4,863
  • 4
  • 38
  • 48
2

Apart from the client side validation with a Validator, I also recommend doing server side validation as well.

bool isValidEmail(string input)
{
    try
    {
        var email = new System.Net.Mail.MailAddress(input);
        return true;
    }
    catch
    {
        return false;
    }
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79