Activity Forums Salesforce® Discussions Restricting Input Text Field declared as Decimal from entering text values

  • Restricting Input Text Field declared as Decimal from entering text values

    Posted by Ravi on July 8, 2016 at 10:29 am

    I have a input text field declared as Decimal and that text field should be restricted from entering text values.
    So now when a text value is entered it is showing the Salesforce standard error "value cannot be converted from text to Decimal", I want to make it customized (showing my own error). Kindly assist.

    • This discussion was modified 7 years, 8 months ago by  Ravi.
    • This discussion was modified 7 years, 8 months ago by  Ravi.
    • This discussion was modified 7 years, 8 months ago by  Forcetalks.
    • This discussion was modified 7 years, 8 months ago by  Forcetalks.
    Prafull replied 7 years, 8 months ago 3 Members · 2 Replies
  • 2 Replies
  • Jogender

    Member
    July 8, 2016 at 10:51 am

    Hello Ravi,

    In your "Submit" method in controller , you can add your customized error message on the page using the below code:

    apexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Enter only numeric values'));

    Hope this helps.

  • Prafull

    Member
    July 8, 2016 at 1:03 pm

    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

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos