Email Format Validation in Apex


Apex provides patterns and matchers that enable you to search text using regular expressions.
And you can use that to validated, if the entered email address is valid or not in apex using the below code.Give a try!

Code:


Pattern p = Pattern.compile( '([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}))');
         Matcher m = p.matcher(leadVar.Email);
         if (!m.matches()) {
            System.debug('Please enter a valid email address');
                    return null;
         }

Ref:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_pattern_and_matcher_using.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_pattern_and_matcher_pattern_methods.htm


P.S: Comment Below for any Clarification or help!!


Happy working!! :) :)

Comments

Popular posts from this blog

Embed Special Characters in VF Page

Some Common Errors in the Visual flow

Radio Button using input tag with type=radio in VF page and action support tag