Home > Net >  how to perform Email Validation and restrict personal email id in c# code?
how to perform Email Validation and restrict personal email id in c# code?

Time:12-12

I want to check whether email is valid or not . Then check if it's personal email like gmail, yahoo etc.

I can do that with JavaScript but i want to do it in c# side

CodePudding user response:

Can use MailAddress

    var trimmedEmail = email.Trim();
   if (string.IsNullOrEmpty(email) || trimmedEmail.EndsWith(".") )
    return false;

    try {
        var addr = new System.Net.Mail.MailAddress(email);
        return addr.Address == trimmedEmail;
    }
    catch {
        return false;
    }

OR

EmailAddressAttribute

var email = new EmailAddressAttribute();
email.IsValid(email);

If it is true then check it is personal or not by

string[] personalEmail = { "gmail","yahoo" };
string email = "[email protected]";
var isPersonal= personalEmail.Any(x => email.Contains(x));
  • Related