function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Roy SinghRoy Singh 

Email Validation format

Hi All,

Could anyone help me to write email validation.

Below these Email format should not be included
Invalid email addresses
Abc.example.com (no @ character)
234567890123456789012345678901234567890123456789012345678901734+x@example.com (too long)
john..doe@example.com (double dot before @)
example@localhost (sent from localhost)
john.doe@example..com (double dot after @)
john..doe@example.com

Please help me guys.
​Thanks in advance
Praful_GuptaPraful_Gupta
String emailString = 'testemail@localhost..com';
Boolean validemail = true;
if(!emailString.contains('@'))
{
     if(emailString.substringAfter('@').contain('localhost'))
     {
         validemail  = false;
      }
      if(emailString.substringAfter('@').contain('..'))
     {
          validemail  = false;
     }
     if(emailString.substringBefore('@').contain('..'))
     {
          validemail  = false;
     }
}else
{
        validemail = false;
}

if(!validemail )
{
addError

}

You need to use https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm to cover all your scenarios