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
pallipalli 

code for Email validation in apex class

Hi, 

 

How to write a code for Email validation in apex class ....

 

this is my page

============

<apex:page controller="JstestController">
<apex:form id="myform">
<apex:pagemessages />
<apex:pageBlock id="myblock">
Email Address: <apex:inputText value="{!email}" id="email"/><br/><br/>
<apex:commandButton value="Click me!" action="{!checkEmail}"/>
</apex:pageBlock>
</apex:form>
</apex:page>

 

class

.....................

 

public class JstestController
{
public String email { get; set; }
public PageReference checkEmail()
{

}

}

 

how to wrete email validation for input text  field ....

help me.......

 

Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645

and yet another ...

 

public static Boolean validateEmail(String email) {
	Boolean res = true;
		
	
	String emailRegex = '^[a-zA-Z0-9._|\\\\%#~`=?&/$^*!}{+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$'; // source: http://www.regular-expressions.info/email.html
	Pattern MyPattern = Pattern.compile(emailRegex);
	Matcher MyMatcher = MyPattern.matcher(email);

	if (!MyMatcher.matches()) 
	    res = false;
	return res;	
	}

 

All Answers

RishiKaliaRishiKalia

Hi Palli,

 

Try following Class :

 

public class JstestController
{
public String email { get; set; }
public PageReference checkEmail(){
system.debug('#####email:'+email);
String emailPattern = email;
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);
Matcher MyMatcher = MyPattern.matcher(email);
system.debug('#####validEmail :'+ MyMatcher.matches() );


return null;
}
}

 

Salesforce has standard pattern matching class. I hope above mention code will help you.

 

Thanks

Rishi kalia

MagulanDuraipandianMagulanDuraipandian

                if(Pattern.matches('[a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z]{2,4}', email))
                {
                    success
                }
                else
                {
                    error       
                }

 

Regards,

Magulan D

Salesforce.com certified Force.com Developer.

SFDC Blog

 

If this post is your solution, kindly mark this as the solution.

Hengky IlawanHengky Ilawan

Hi,

 

Why don't you use the built-in email field type?

 

Regards,

Hengky

crop1645crop1645

and yet another ...

 

public static Boolean validateEmail(String email) {
	Boolean res = true;
		
	
	String emailRegex = '^[a-zA-Z0-9._|\\\\%#~`=?&/$^*!}{+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$'; // source: http://www.regular-expressions.info/email.html
	Pattern MyPattern = Pattern.compile(emailRegex);
	Matcher MyMatcher = MyPattern.matcher(email);

	if (!MyMatcher.matches()) 
	    res = false;
	return res;	
	}

 

This was selected as the best answer
zachbarkleyzachbarkley

Hi All,

 

MAGU's marked solution does not work 100% . It does not cater for john@doe (NO .com). Crop1645 is a more complete solution. Would you please update solution to correct solution.

Ben Burbridge 7Ben Burbridge 7
Crop1645's solution also fails on john@doe..com

Regards, Ben
Ken Koellner @ EngagewareKen Koellner @ Engageware
Crop1645's solution allows grave accent ` ASCII 96 but not apostrophe ' ASCII 39.  However email address can have ' prior to the @.