Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP ASP.NET Javascript jQuery , +
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.
<%@ 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 28-Nov-12 13:49pm
Edited 28-Nov-12 13:50pm
v2
Comments
Mathlab at 28-Nov-12 20:30pm
   
String.Format()
sreeharshakakarla at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
<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
  Permalink  
Comments
sreeharshakakarla at 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)

  Print Answers RSS
0 OriginalGriff 6,631
1 Sergey Alexandrovich Kryukov 6,345
2 CPallini 5,230
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 29 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100