Monday, 10 November 2014

how to check valid email Address in Apex Salesforce

String emailRegex = '([a-zA-Z0-9_\\-\\.]+)@((\\[a-z]{1,3}\\.[a-z]{1,3}\\.[a-z]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})';
Pattern MyPattern = Pattern.compile(emailRegex);

// Then instantiate a new Matcher object "MyMatcher"
String InputString ='abc@xyzzzz.com';
Matcher MyMatcher = MyPattern.matcher(InputString);

if (!MyMatcher.matches()) {
// invalid, do something
   System.debug('Invalid Email Addresss');
}
else
{
      System.debug('Valid Email Addresss');
}

4 comments:

  1. Does not work when email starts with two dots, i.e. "..lastname@somewhere.com".
    Regex says "valid", salesforce says "invalid"

    ReplyDelete
    Replies
    1. Please check How to Check email address is valid in apex post.

      Delete
    2. https://apexin3hands.blogspot.in/2015/09/how-to-check-email-address-is-valid-in.html?m=1

      Delete
  2. Thank you again for all the knowledge you distribute,Good post. I was very interested in the article, it's quite inspiring I should admit. I like visiting you site since I always come across interesting articles like this one.Great Job, I greatly appreciate that.Do Keep sharing! Regards, verifications.io

    ReplyDelete