Home > Blockchain >  MailAddress checking
MailAddress checking

Time:11-04

Hi Im trying to check if my email is valid or not, and wanted to add a restriction that invalidates an email if the @ is followed by a period (.)

I have made/use some of this restriction but I want to add more restriction to it. I need your help on how to declare it.

Cheers

private bool IsValidEmail(string email)
    {

        MailAddress mailAddress = new MailAddress(email);

        var hostParts = mailAddress.Host.Split('.');
        if (hostParts.Length == 1)
            return false; // No dot.
        if (hostParts.Any(p => p == string.Empty))
            return false; // Double dot.

        if (mailAddress.User.Contains(' '))
            return false;
        if (mailAddress.User.Split('.').Any(p => p == string.Empty))
            return false; // Double dot or dot at end of user part.

        return true;

    }

this is my code so far

CodePudding user response:

the best way to validate anything is to know the syntax.

Here you can find the Syntax of an E-Mail Adress: Wiki

  • Related