1

I have used the following code for the email validation and the validation is working fine but I would like to show the error like the the image. how to do that?

My Code:

 <apex:pageBlock id="myblock">
       Email  <apex:inputText value="{!email}" id="email"/><br/><br/>
        <apex:commandButton value="Click me!"  action="{!checkEmail}"/>
 </apex:pageBlock>

Apex:

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

My expected View:

enter image description here

1

Try below code:

<apex:pageBlock id="myblock">
    Email <apex:inputText value="{!email}" id="email"/><br/><br/>
    <div class="errorMsg">
        <strong></strong>&nbsp;{!emailError}
    </div>
    <apex:commandButton value="Click me!"  action="{!checkEmail}"/>
</apex:pageBlock>

Apex:

if(!Pattern.matches('[a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z]{2,4}', email))
        {
         emailError ='Either first name or middle name must be defined';
        }
0

You can't use the apex pagemessages functionality to achieve this - you'll have to roll your own to a degree, capturing the errors in controller properties and conditionally rendering markup to decorate the fields in error.

I wrote a blog post on this very topic some time ago:

http://bobbuzzard.blogspot.co.uk/2011/04/field-level-error-messages-with_29.html

  • I got this error: Error: testController Compile Error: expecting right curly bracket, found 'for' at line 12 column 5 for (testController wrap : wrappers) { if(!Pattern.matches('[0-9]{3}-[0-9]{3}-[0-9]{4}', email)) { wrap.emailError ='Either first name or middle name must be defined'; } } – Lemon Aug 28 '14 at 7:22
  • My COde is for (testController wrap : wrappers) { if(!Pattern.matches('[0-9]{3}-[0-9]{3}-[0-9]{4}', email)) { wrap.emailError ='Either first name or middle name must be defined'; } } – Lemon Aug 28 '14 at 7:22
  • That doesn't appear to be related to the question that you asked - its a regular expression issue. You should raise that as a separate question rather than tacking it on in the comments. – Bob Buzzard Aug 28 '14 at 7:23
0

I think you can create a new instance of the ApexPages.Message class using the following signature.

public Message(ApexPages.Severity severity, String summary, String detail, String id)

And then add this ApexPages.Message instance to ApexPages using ApexPages.addmessage.

Note ID should be id of your field where you want to show the Error.

For more details refer

https://www.salesforce.com/us/developer/docs/pages/index_Left.htm#CSHID=apex_methods_system_apexpages.htm|StartTopic=Content%2Fapex_methods_system_apexpages.htm|SkinName=webhelp

Let me know if you face any issue.

  • That link goes to "Introducing visualforce", could you be more specific please? – flor Aug 30 '16 at 22:24
0

The standard email field can validate email format automatically, we can use that. For example, you can declare Account instance, then bind Account.Email with . If the email you typed is invalid, the error is 'Error: Invalid Email Address'. Then you can get email value in your apex code.

This is the easiest way i have known.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.