Activity Forums Salesforce® Discussions present validation error under inputtext field in Salesforce

  • Avnish Yadav

    Member
    September 4, 2018 at 7:23 am

    Hi Jos,

    This will help you,

    Controller Page

    public with sharing class Regpagedemo1ctrl {
    public String emailError{get;set;}
    public String email{get;set;}
    public String passwordError{get;set;}
    public String password{get;set;}
    public Boolean shwErrorMail{get;set;}
    public Boolean shwErrorPswd{get;set;}

    public Regpagedemo1ctrl(){
    shwErrorMail = False;
    shwErrorPswd = False;
    }

    public PageReference checkEmail() {
    shwErrorMail = False;
    shwErrorPswd = False;
    if(email == ''){
    emailError ='ENTER EMAIL';
    shwErrorMail = True;
    }

    if(password == ''){
    passwordError = 'ENTER PASSWORD';
    shwErrorPswd = True;
    }

    return null;
    }}

    Vf page:-

    <apex:page controller="Regpagedemo1ctrl" >
    <apex:form id="mnFrmId">
    <apex:pageBlock id="mnPgBlkId">
    <table id="TblId">
    <tr>
    <td>
    <apex:outputText > Email Address: </apex:outputText>
    </td>
    <td>
    <apex:inputText id="myEmlId" value="{!email}"/>
    </td>
    </tr>
    <tr>
    <td></td>
    <td>
    <apex:outputText rendered="{!shwErrorMail}"> <span style="color:red;">{!emailError}</span></apex:outputText>
    </td>
    </tr>

    <tr>
    <td>
    <apex:outputText value="Password: "/>
    </td>
    <td>
    <apex:inputText id="myPswdId" value="{!password}"/>
    </td>
    </tr>
    <tr>
    <td></td>
    <td>
    <apex:outputText rendered="{!shwErrorPswd}"> <span style="color:red;">{!passwordError}</span></apex:outputText>
    </td>
    </tr>
    <tr>
    <td>
    <apex:commandButton value="Click me!" action="{!checkEmail}" reRender="mnFrmId"/>
    </td>
    </tr>
    </table>
    </apex:pageBlock>

    </apex:form>

    Thanks.

  • Neha

    Member
    September 4, 2018 at 11:02 am

    Hi Jos,

    Here's the code! Maybe it's helpful for others
    VF page:

    <apex:outputLabel value="Postcode"></apex:outputLabel><br><apex:inputText styleClass="{!classInputPostalCode}" value="{!postalCode}"/><br><apex:outputText value="{!errorMessage}" styleClass="errorMsg" rendered="{!LEN(errorMessage)>0}"/>

     Controller code:

    public String classInputPostalCode {get;set;}<br>public String postalCode {get;set;}<br>public String errorMessage {get;set;}<br><br>public void validate() {<br> if (error) {<br> classInputPostalCode = 'error'; // put the errorclass, red borders<br> errorMessage = 'Postal code not found';<br> } else {<br> classInputPostalCode = '';<br> errorMessage = '';<br> }<br>}

     

  • Parul

    Member
    September 20, 2018 at 6:02 pm

    Hi,

    You can use following code in your apex controller to get error message to respective field.

    ObjectInstance.FieldName.AddError('Your Error message')

    for example if you have contact object and you want to display error message in lastName field then you can do like following

    Contact con = new contact();

    con.LastName.addError('Please fill contact Name');

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos