1

For Example i need to add two numbers? So how is it possible with

   <apex:inputText value="somevalue" action ="{!action}"/>

With this single inputText and On clicking the button , the values in the text field should be passed to the controller class and generates output.

3
  • So, for example, you are entering "1,2" in your text field and on button click you should get 3 ?
    – sf_user
    Commented Sep 19, 2015 at 11:09
  • Yes but using only one inputText Field.
    – Vivek C
    Commented Sep 19, 2015 at 11:15
  • Suppose if You Want to Create a calculator using single <apex:inputText value="somevalue" /> how?
    – Vivek C
    Commented Sep 19, 2015 at 11:30

2 Answers 2

1

Try This:

Apex Page:

<apex:page docType="html-5.0" controller="testing">
    <apex:form >
        <apex:input value="{!addthis}"/>
        <apex:commandButton action="{!add}" value="Add"/>
        <apex:outputLabel >{!result}</apex:outputLabel>
    </apex:form>
</apex:page>

Controller:

public class testing {

    public Integer result { get; set; }
    public String addthis{get; set;}
    public List<Integer> IntList = new List<Integer>();

    public PageReference add() {
        Integer sum = 0;
        List<String> str = addthis.replace(' ','').split(',');
        for(String s : str){
            Integer i = Integer.ValueOf(s);
            sum = sum +i;
        }
        result = sum;
        return null;
    }
}

Update: If you want result in same input text:

Page:

<apex:page docType="html-5.0" controller="testing">
    <apex:form >
        <apex:input value="{!addthis}"/>
        <apex:commandButton action="{!add}" value="Add"/>
    </apex:form>
</apex:page>

Controller:

public class testing {

    //public Integer result { get; set; }
    public String addthis{get; set;}
    public List<Integer> IntList = new List<Integer>();

    public PageReference add() {
        Integer sum = 0;
        List<String> str = addthis.replace(' ','').split(',');
        for(String s : str){
            Integer i = Integer.ValueOf(s);
            sum = sum +i;
        }
        addthis = sum+'';
        return null;
    }

}
2
  • What should i do to display value in same inputText Field?
    – Vivek C
    Commented Sep 19, 2015 at 12:18
  • instead of outputlabel line, use <apex:inputText value="{!result}"/> . Oops I didnt read the question updated my answer
    – sf_user
    Commented Sep 19, 2015 at 12:20
0

action is not a supported attribute of <apex:inputText>

Not sure why you want to get this but here is the example code: Page:

<apex:inputText value="myText"/>
<apex:commandButton value="submit" action="{!submit}"/>

Controller:

public class theController 
{
    public String myText {get; set;}
    public Integer actualField {get; set;}

    public void submit()
    {
        public List<String> numberList = myText.split(',');
        if(numberList.size() == 0)
            actualField = 0;
        else if(numberList.size() == 1)
            actualField = Integer.valueOf(numberList[0]);
        else if(numberList.size() > 1)
            actualField = Integer.valueOf(numberList[0] + numberList[1]);
        //You need to write the error handling code yourself
        //...

    }
}

You must log in to answer this question.

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