ASP.NET email validator regex

130,120

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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

Share:
130,120

Related videos on Youtube

JamesBrownIsDead
Author by

JamesBrownIsDead

Updated on August 19, 2020

Comments

  • JamesBrownIsDead
    JamesBrownIsDead almost 4 years

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

    • viplove
      viplove over 14 years
      I don't think there is a built-in "EmailValidator" control you can use. You can roll your own using the RegularExpressionValidator.
    • LCJ
      LCJ over 11 years
      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.
  • Bart Kiers
    Bart Kiers over 14 years
    Is that how it's done in ASP.NET's email-validator? That'd match _@_._... I'd expected something more fancy.
  • Josh Mein
    Josh Mein over 14 years
    @Bart check it out for yourself. I told you how I found it
  • Bart Kiers
    Bart Kiers over 14 years
    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
    Bart Kiers over 14 years
    Besides, validating e-mail addresses is just a waste of time, IMO.
  • IrishChieftain
    IrishChieftain over 14 years
    Validating email addresses on the form is never a waste of time. You can still use it in conjunction with other checks.
  • JamesBrownIsDead
    JamesBrownIsDead over 14 years
    Thanks for telling me how to find it!
  • Bart Kiers
    Bart Kiers over 14 years
    Well IrishChieftain, it seems we have a different opinion. Note the 'IMO' after my statement. Something that should also belong after yours... IMO. :)
  • Stilgar
    Stilgar about 12 years
    @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 - jacobsantos.com/2007/general/…
  • The Muffin Man
    The Muffin Man almost 12 years
    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.
  • LCJ
    LCJ over 11 years
    You could possibly provide a sample for the simple validation with @ and . ( or refer stackoverflow.com/questions/742451/…)
  • capdragon
    capdragon about 11 years
    You need both. You shouldn't have waist your time sending an email out until you at-least know it's a valid email.
  • The Muffin Man
    The Muffin Man over 10 years
    That regex is pure awesomeness
  • The Muffin Man
    The Muffin Man over 10 years
    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.
  • oligofren
    oligofren over 8 years
    -1 for providing a solution that incorrectly identifies valid emails as bad. Compare your regex to this: ex-parrot.com/pdw/Mail-RFC822-Address.html You should add a warning that this solution is too simplistic.
  • Guillermo Varini
    Guillermo Varini over 8 years
    should u check if there is special characters before @ ? stackoverflow.com/questions/2049502/…
  • Josh Mein
    Josh Mein over 8 years
    @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.
  • SteveCav
    SteveCav over 4 years
    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.