2

Currently I am using following code for email validation but it does validate the dsgf@g mail id please help me

[Required(ErrorMessage = "Please Enter Email Id")]
[Display(Name = "Email-Id")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string Cust_Email { get; set; }
mmushtaq
  • 3,430
  • 7
  • 30
  • 47
Ravi
  • 25
  • 1
  • 6
  • dsgf@g could not be a correct email address, a valid email address needs to be like this: name@domain.tld. Plus you need to make it email address data type (for formatting) and validate it with EmailAddress attribute. See this: http://stackoverflow.com/questions/8989081/email-model-validation-with-dataannotations-and-datatype – Fourat Aug 29 '16 at 08:24
  • 3
    @Fourat `dsgf@g` is a completely valid email address, so there is nothing wrong with that method. Consider the example `username@localhost` for example – Roman Marusyk Aug 29 '16 at 08:30

3 Answers3

2

EmailAddress attribute will mark as valid the dsgf@g because it is a completely valid email address, so there is nothing wrong with that method. Consider the example username@localhost for example.

If it is not suits you then you can use regular expression ti set your own rule for validation. Try to use 'RegularExpression' attribute instead, something like:

[RegularExpression("^[^@\s]+@[^@\s]+(\.[^@\s]+)+$", ErrorMessage = "Invalid Email Address")]
public string Cust_Email { get; set; }

or

[RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Invalid Email Address")]
public string Cust_Email { get; set; }
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
  • maximum I'd do with regex to validate the mail-address, is checking if there's 1 '@' character. In his case eventually checking for the '.' character after the '@'. validating email with regex is imho a bad practice. See my answere with the link to the regex (which is outdated, too. maybe you understand, what I mean) – Matthias Burger Aug 29 '16 at 09:16
  • @MatthiasBurger I do not think that a validating email with regex is a bad practice. If you not write international service then you can very easy check mail with regex. It is using in each second service and application. And RFC822 is very scared, please see at this : http://blog.onyxbits.de/validating-email-addresses-with-a-regex-do-yourself-a-favor-and-dont-391/ – Roman Marusyk Aug 29 '16 at 09:32
  • `EmailValidator`-class scares me, too. :D But would you really take the time to get the correct regex instead of switching to a simplier solution and send him an email with a code or link to make sure his mail-address is valid? (remember, that possibilities for email-addresses changed a few times. You would have to change your regex too, for supporting the new standard - everytime). Plus, with sending a mail you have the advantage to see if the address exists. – Matthias Burger Aug 29 '16 at 09:43
  • @MatthiasBurger I did not say that your solution is bad. It is a good approach. I've just said that regex is simpler way :) and the question is about validation using attributes – Roman Marusyk Aug 29 '16 at 09:49
0

the email is completely valid.

if you want to validate, simply don't use regex for validation. Send him a code to this email-address that he has to enter. email-addresses can now contain characters like ä,ö,ü,à,... this could be really difficult to match the correct one.. if you really want to validate it using regex you could take the RFC822 standard then: you will find here: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html (have fun with this - didn't want to post, it's too long)

Matthias Burger
  • 5,549
  • 7
  • 49
  • 94
0

What about an extension method.

    public static bool IsValidEmail(this string email)
{
    bool rt = false;
    try
    {
        var mail = new System.Net.Mail.MailAddress(email);
        rt = mail.Host.Contains(".");
    }
    catch { }
    return rt;
}
Mike
  • 173
  • 2
  • 5