Hi Ravi,
I am showing the solution below with the help of a VF page and its controller.
Visualforce page :-
<apex:page standardController=”Account” extensions=”VfPageErrorMessageController”>
<apex:form >
<apex:pageblock >
<apex:pageMessages id=”showmsg”></apex:pageMessages>
<apex:panelGrid columns=”2″>
Account Name: <apex:inputText value=”{!acc.name}”/>
Account Number: <apex:inputText value=”{!acc.AccountNumber}”/>
Account Phone: <apex:inputText value=”{!acc.phone}”/>
Account Site: <apex:inputText value=”{!acc.site}”/>
Account Industry: <apex:inputText value=”{!acc.industry}”/>
<apex:commandButton value=”Update” action=”{!save}” style=”width:90px” rerender=”showmsg”/>
</apex:panelGrid>
</apex:pageblock>
</apex:form>
</apex:page>
I have considered that “acc.AccountNumber” is declared as Decimal.
Controller :-
public class VfPageErrorMessageController {
public Account acc{get;set;}
public VfPageErrorMessageController(ApexPages.StandardController controller) {
acc = new Account();
}
public class DecimalResult{
public Boolean IsValid{ get; private set; }
public Decimal Value { get; private set; }
public DecimalResult(Boolean pIsValid, Decimal pValue){
IsValid = pIsValid;
Value = pValue;
}
public DecimalResult(Boolean pIsValid){
IsValid = pIsValid;
Value = null;
}
}
public static DecimalResult toDecimal(String strInput){
DecimalResult result = null;
try {
Decimal decimalValue = Decimal.valueOf(strInput);
result = new decimalResult(true, decimalValue);
}
Catch (exception e) {
result = new DecimalResult(false);
}
return result;
}
public void save(){
DecimalResult res2 = toDecimal(acc.AccountNumber);
if(!res2.IsValid){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,’Please enter valid decimal value’));
}
}
}
Hope this helps…
Thanks,
Prafull