calculate asp.net textbox as sum and multiply

RAVI 1,076 Reputation points
2023-10-26T12:20:12.46+00:00

Hello

In my asp.net i have a textbox1 in that For example

  1. If i type 22*2 and then if i type = it has to show 44
  2. If i type 10-2 and then if i type = it has to show 8

How to do using javascript without postback..
Thanking You

Microsoft 365 and Office Development Office JavaScript API
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2023-10-26T15:39:54.7566667+00:00

    Try something like this:

    <script>
        function HandleInput(e)
        {
            let t = e.target.value;
            t = t.trim();
    
            if( t.endsWith( "=" ) )
            {
                let v = eval( t.substring( 0, t.length - 1 ) );
    
                alert( v );
            }
        }
    </script>
    
    . . .
    
    
    <input type="text" oninput="HandleInput(event)"/>
    

    The result is shown using the alert function.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2023-10-27T06:31:37.9733333+00:00

    Hi @RAVI,

    How to do using javascript without postback..

    It looks like you are using the TextBox control in a WebForms project, if you don't want it postback, you should set the AutoPostBack attribute to False. Something like this:

    <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="false"></asp:TextBox>
    

    And when using this control, you may be accustomed to using the Enter key to do postback, in which case you can add the following code to disable the Enter key to submit the form, and calculate the desired result using the code that community members provided:

    <script src="Scripts/jquery-3.4.1.min.js"></script>
    <script>
    $('#form1').on('keyup keypress', function (e) {             
    	var keyCode = e.keyCode || e.which;             
    	if (keyCode === 13) {                 
    	e.preventDefault();                 
    	return false;             
    	}         
    });          
    $("#TextBox1").on('keyup keypress', function (e) {             
    	if (e.key === 'Enter' || e.keyCode === 13) {                 
    	// Do something                 
    		let t = e.target.value;                 
    		t = t.trim();                 
    		if (t.endsWith("=")) {                     
    			let v = eval(t.substring(0, t.length - 1));                      
    		$("#TextBox1").val(v);                 
    		}             
    	}         
    });
    </script>
    

    Hope this can help you.

    Best regards,

    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. RAVI 1,076 Reputation points
    2023-10-28T09:17:59.2233333+00:00

    no sir my requirment is in textbox1

    for example if i type 2*3 = then it should write 6 in textbox1

    if i type 4+3= then it should write 7 in textbox1

    hope you understand i need multiply, sum comma in only one textbox


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.