Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hie all,
I have two text boxes which automatically add commas when user enters number. These text boxes are added to third text box. It works fine with commas. But I want all text boxes to automatically format to two decimals including comma (Ex : 1,345.00 + 3,456.00 = 4801.00) .
I got this code which works fine to add text boxes and show result with comma but it does not automatically include two decimal points. Please help me. Thanks in advance.
XML
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm8.aspx.cs" Inherits="Application.WebForm8" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script type="text/javascript" language="javascript">
        function Comma(Num) { //function to add commas to textboxes
            Num += '';
         Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
         Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
         x = Num.split('.');
         x1 = x[0];
         x2 = x.length > 1 ? '.' + x[1] : '';
         var rgx = /(\d+)(\d{3})/;
         while (rgx.test(x1))
         x1 = x1.replace(rgx, '$1' + ',' + '$2');
         return x1 + x2;

     }
     function sumCalc() // function to remove comma and then add to third textbox
{
         var txt1 = document.getElementById('<%=tb1.ClientID %>').value.replace(/,/g, "");
         var txt2 = document.getElementById('<%=tb2.ClientID %>').value.replace(/,/g, "");
         var txt3 = document.getElementById('<%=tb3.ClientID %>').value = Comma(parseFloat(txt1) + parseFloat(txt2));

     }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:TextBox ID="tb1" runat="server" onkeyup = "javascript:this.value=Comma(this.value);" ></asp:TextBox>
    <asp:TextBox ID="tb2" runat="server" onkeyup = "javascript:this.value=Comma(this.value);"></asp:TextBox>
    <br />
    <asp:Label ID="Label1" runat="server" Text="Result"></asp:Label>
    <br />
    <asp:TextBox ID="tb3" runat="server" onfocus="javascript:sumCalc();" ></asp:TextBox>
    <br />
    <br />
    <br />
</asp:Content>
Posted
Updated 28-Nov-12 13:50pm
v2
Comments
[no name] 28-Nov-12 20:30pm    
String.Format()
Slacker89 28-Nov-12 20:36pm    
Could you please elaborate it by pointing out changes that I need to make. I am newbie in java script.
Zaf Khan 28-Nov-12 22:33pm    
With reference to what Mathlab said in his comment above, you can find on this site an article here http://www.codeproject.com/Tips/201899/String-Format-in-JavaScript

1 solution

C#
function addCommas(clientID)
                   {

                       var nStr = document.getElementById(clientID.id).value;

                       nStr += '';
                       x = nStr.split('.');
                       if (!x[0] )
                       {
                           x[0] = "0";

                       }
                       x1 = x[0];
                       if (!x[1] )
                       {
                           x[1] = "00";
                       }
                       x2 = x.length > 1 ? '.' + x[1] : '';
                       var rgx = /(\d+)(\d{3})/;
                       while (rgx.test(x1)) {
                           x1 = x1.replace(rgx, '$1' + ',' + '$2');
                       }

                       document.getElementById(clientID.id).value = x1 + x2;
                       return true;
                   }

                    function removeCommas(clientID)
                   {
                       var nStr = document.getElementById(clientID.id).value;
                       nStr = nStr.replace(/,/g, '');
                       document.getElementById(clientID.id).value = nStr;

                       $(clientID).select();

                       return true;
                   }

And call it on onfocus
XML
<asp:TextBox ID="txtbxId" runat="server" onfocus="removeCommas(this)" OnBlur="addCommas(this)"></asp:TextBox>

This will be format your number with commas and decimals
be carefull while accessing textbox value, remove commas before use
 
Share this answer
 
Comments
Slacker89 29-Nov-12 12:30pm    
Hie Manoj, Thanks for your solution. It works fine but still I am not able to sum using your functions. I have problem in sending client id to sum function. Please help me with this. Appreciate your time
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript" language="javascript">

function sumCalc() { // function to remove comma and then add to third textbo

var txt1 = document.getElementById('<%=tb1.ClientID %>').value.replace(/,/g, '');
var txt2 = document.getElementById('<%=tb2.ClientID %>').value.replace(/,/g, '');
var txt3 = document.getElementById('<%=tb3.ClientID %>').value = addCommas(parseFloat(txt1) + parseFloat(txt2));
}
function addCommas(clientID) {
var nStr = document.getElementById(clientID.id).value;
nStr += '';
x = nStr.split('.');
if (!x[0]) {
x[0] = "0";
}
x1 = x[0];
if (!x[1]) {
x[1] = "00";
}
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}

document.getElementById(clientID.id).value = x1 + x2;
return true;
}

function removeCommas(clientID) {
var nStr = document.getElementById(clientID.id).value;
nStr = nStr.replace(/,/g, '');
document.getElementById(clientID.id).value = nStr;

$(clientID).select();

return true;
}

</script>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:TextBox ID="tb1" runat="server" onfocus="removeCommas(this)" OnBlur="addCommas(this)">
<asp:TextBox ID="tb2" runat="server" onfocus="removeCommas(this)" OnBlur="addCommas(this)">
<br />
<asp:Label ID="Label1" runat="server" Text="Result">
<br />
<asp:TextBox ID="tb3" runat="server" onfocus="javascript:sumCalc();">
<br />
<br />
<br />

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900