1

Requirement:Show only one validation error at a time

I created a vf page which contains an inputtext field "Email" .My requirement is that when I am keeping the field blank it should show an error "ENTER EMAIL" and again when I have entered any charecter in the field and it doesnot matches the proper email format then the error to be shown is "ENTER EMAIL IN CORRECT FORMAT".Right now whenever I am keeping the field blank both the errors are displayed whereas I need to display only the "ENTER EMAIL" error in this case.I am pasting my code here .Please guide me with this.

<apex:page controller="EmailLoginController">
<apex:form >
    <div id="emailoutputLabel">
        <apex:outputText > Email </apex:outputText>
    </div>
    <div id="emailinputText">
        <apex:inputText id="email" value="{!email}" />
    </div>
    <div id="emailOutputTextRender">
        <apex:outputText rendered="{!shwErrorNullEmail}"> <span style="color:red;">{!emailNullError}</span></apex:outputText>
    </div>
    <div id="emailFormatOutputTextRender">
        <apex:outputText rendered="{!shwErrorFormatEmail}"> <span style="color:red;">{!emailFormatError}</span></apex:outputText>
    </div>
    <apex:commandButton action="{!registerUser}" value="REGISTER NOW" id="submit" />
</apex:form>

public with sharing class EmailLoginController {

public String email {get; set;}
public String emailNullError{get;set;}
public String emailError{get;set;}
public String emailFormatError{get;set;}
public Boolean shwErrorNullEmail{get;set;}
public Boolean shwErrorEmail{get;set;}
public Boolean shwErrorFormatEmail{get;set;}

public EmailLoginController () {
    shwErrorNullEmail=False;
    shwErrorFormatEmail=False;
}

public PageReference registerUser() {
    shwErrorNullEmail=False;
    shwErrorFormatEmail=False;

    if(email == ''|| email == null) {
      emailNullError='ENTER EMAIL';
      shwErrorNullEmail=True;
   }

   if((email != null)&&(!Pattern.matches('[a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z]{2,4}[.]{0,1}[a-zA-Z]{0,2}', email))){          
      emailFormatError='ENTER EMAIL IN CORRECT FORMAT';
      shwErrorFormatEmail= True;
   }

   return null;
}

}

3 Answers 3

2

A better way is to use if...else if... construct instead of if... if... construct as you want to show only an error message at a time, for example:

if(String.isBlank(email)){
    emailNullError='ENTER EMAIL';
    shwErrorNullEmail=True;
}else if(!Pattern.matches('[a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z]{2,4}[.]{0,1}[a-zA-Z]{0,2}', email)){
    emailFormatError='ENTER EMAIL IN CORRECT FORMAT';
    shwErrorFormatEmail= True;
}

And with this, there is no need to add multiple return null; statements.

1

You are doing the correct change but need to little bit more changes :- while you are giving blank values it should checked in your second if block like below : -

if((email != null && email != '')&&(!Pattern.matches('[a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z]{2,4}[.]{0,1}[a-zA-Z]{0,2}', email))){          
      emailFormatError='ENTER EMAIL IN CORRECT FORMAT';
      shwErrorFormatEmail= True;
   }

it should also work.

1

Simply return from if block if that condition is satisfied, as follows:

if(String.isBlank(email)){
      emailNullError='ENTER EMAIL';
      shwErrorNullEmail=True;
      return null;
 }
4
  • Hii Rohit, Thank you for your answer >now the functionality is working properly May 2, 2017 at 5:30
  • @SaswataDas Then accept the answer. May 2, 2017 at 5:32
  • 2
    We can use if(String.isBlank(email)){ instead of if(email == ''|| email == null) {; May 2, 2017 at 5:42
  • @itzmukeshy7 Yes using String.isBlank() makes more sense. May 2, 2017 at 5:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .