Ads

Friday 3 May 2013

Email validation using Apex in Salesforce?

Visualforce page:

<apex:page controller="testController">
    <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>


Apex Code:

public class testController
{
    public String email { get; set; }
    public void checkEmail()
    {

        if(!Pattern.matches('[a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z]{2,4}', email))
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Check your email')); 

        }
    }
}

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. What would be the code to manage emails with the following format: example@yahoo.com.mx?

    ReplyDelete
  3. I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day! Verifications IO

    ReplyDelete
  4. Thanks. Great solution. Much appreciated.

    ReplyDelete