+ Start a Discussion
MariPMariP 

in a class, check a field is a valid email address

Hi everybody,

 

The users can populate a text area with free email addresses.

I put them in a list (I suppose they have put ";" as a separator between each elements)

 

How can I check each one is a valid email address ?

 

Here is my code :

 

 

// add user free field to  the list of emails	 
 if (closedRequest.Others_email_addresses__c!= null) { 	

list<string> lEmailOthersAddressesList = closedRequest.Others_email_addresses__c.split(';', 15);

for(String sOthersAddresses : lEmailOthersAddressesList){
lEmailAddressesList.add(sOthersAddresses);
System.debug('valeur de lEmailAddressesList = ' + lEmailAddressesList);
}
}

 Is it possible to test sOthersAddresses to see if it is a valid address email ?

 

Thanks for any help !

Marie

 

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

You can use regular expressions to check that the email address is in the correct format.

http://salesforcesource.blogspot.com/2010/01/utilizing-apex-pattern-and-matcher.html

 

String InputString = 'email@email.com';
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"
Matcher MyMatcher = MyPattern.matcher(InputString);

if (!MyMatcher.matches()) {

All Answers

Ritesh AswaneyRitesh Aswaney

You can use regular expressions to check that the email address is in the correct format.

http://salesforcesource.blogspot.com/2010/01/utilizing-apex-pattern-and-matcher.html

 

String InputString = 'email@email.com';
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"
Matcher MyMatcher = MyPattern.matcher(InputString);

if (!MyMatcher.matches()) {
This was selected as the best answer
MariPMariP

thanks a lot ! 

 

Have a nice day !

Wim van BeekWim van Beek

Great option, But ....

 

The TDL looks to be defined as max 3 positions, me@somedomain.info is 4 and other TDL are possible now (or near future) as .amsterdam etc.

As I am not familiar with regulair expresions, can someone please modify this string so it can stand the new requirments for email.

 

Thanks.

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})';