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
Akshay AmbhureAkshay Ambhure 

Show add error message on Visualforce Page is not working

Kindly help me out with below code
Except Error message functionality all working

//VFP
<apex:page controller="NumberClassErrorMessage" wizard="true">
    <apex:form >
        <apex:pageBlock title="Number Calculations">
            <apex:pageMessages id="showmsg"></apex:pageMessages>
            
            <apex:pageBlockSection title="Calculator" >
                <font color="#E70E38">
                Enter Number 1: <apex:inputText value="{!Num1}" />
                Enter Number 2: <apex:inputText value="{!Num2}"/>
                    </font>
                <apex:commandButton value="Calculate" action="{!calculator}" reRender="xyz" />
                <apex:outputLabel id="xyz" value="{!SUBSTITUTE(JSENCODE(message), '\\n', '<br/>')}" escape="false" />       
            </apex:pageBlockSection>    
        </apex:pageBlock>
    </apex:form>    
</apex:page>

//APEX
public class NumberClassErrorMessage {
    
    public integer Num1 {set; get;}
    public integer Num2 {set; get;}
    public string message {set; get;}
    public void calculator()
    {
        integer add,sub,mul,div ;
        
        //if(acc.AccountNumber == '' || acc.AccountNumber == null)
        //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Account number'));
        /*if(NumberA < 0){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'First number is negative!'));
}
*/      
        if(Num1 == null || Num1 < 0){
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, 'Kindly Enter Valid Amount in Number 1'));
            
        }
        if(Num2 == null || Num2 < 0){
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, 'Kindly Enter Valid Amount in Number 2'));
        }
        
        add = Num1+Num2;
        sub = Num1-Num2;
        mul = Num1*Num2;
        // div = Num1/Num2;
        
        message = 'Addition of Numbers :' +add + '\n' + 
            'Substraction of Numbers :' +sub +  '\n' +
            'Multiplication of Numbers :' +mul +  '\n' +
            'Division of Numbers :'  ; // +div
    }
}
Suraj Tripathi 47Suraj Tripathi 47
Hi  Akshay,
It seems that your code is correct and if error message is not showing so put system debug inside your if block and 
check if that debug is print or not in log after execute the VF page.

If you find your Solution then mark this as the best answer.

Thank you!
Regards,
Suraj Tripathi  
Dushyant SonwarDushyant Sonwar
Akshay,

Change this line 
<apex:commandButton value="Calculate" action="{!calculator}" reRender="xyz" />

to
<apex:commandButton value="Calculate" action="{!calculator}" reRender="xyz,showmsg" />

This will work​​​​
Dushyant SonwarDushyant Sonwar
Akshay,

Did you try with above code? Does it solve your purpose?