1
votes

I am using the asp.net validation control to validate email addresses. There was an issue with an email address because it has the following characters. "-." For example the email address w.-a.wsdf.com will not validate due to the ".-" in it. I was looking around for email standards that forbid this but could not find any. Should I change the asp.net regex to a custom one to allow this or is this not a valid email address?

The regex i am using now is : \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

5
Just curious if you did not test it?JNL
See also this question (from the "Related" section): stackoverflow.com/q/201323/121309Hans Kesting

5 Answers

0
votes

You'll get loads of answers for this. This one has always served me well:

/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

Originally found it in Michael Hartl's Rails book

0
votes
^[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]$
0
votes

Regular expressions are great but, as my comments showed in other answers, validating email addresses with them is not an easy task at all.

Based on my experience, I would suggest to either:

  • stick to the simplest email validation regex, ^.+@.+$ and send a confirmation email with an activation link;

or

  • avoid using regular expressions at all and use a library like my EmailVerify for .NET, which exposes a custom, fine tuned finite state machine for validating email addresses according to (all of) the related IETF standards plus disposable, DNS, SMTP, mailbox tests.

Obviously you can mix the two alternatives and perhaps avoid using regular expressions but send a confirmation link anyway.

Disclaimer: I am the tech lead of EmailVerify for .NET, a Microsoft .NET component for validating email addresses.

0
votes

The regex below forbids w.-a.wsdf.com

using System.Text.RegularExpressions;

public static bool IsValidEmail(string email)
{
    return Regex.IsMatch(email, @"\A[a-z0-9]+([-._][a-z0-9]+)*@([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,4}\z")
        && Regex.IsMatch(email, @"^(?=.{1,64}@.{4,64}$)(?=.{6,100}$).*");
}

See validate email address using regular expression in C#.

-1
votes

Take a text input in html and a button input like this Now when the button is clicked then the JavaScript function SubmitFunction() will be called. Now write the bellow code in this function.



function checkEmail() {

    var email = document.getElementById('txtEmail');
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if (!filter.test(email.value)) {
    alert('Please provide a valid email address');
    email.focus;
    return false;
 }
}

*its works *