Click here to Skip to main content
15,891,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to separate integer with commas in while entering the numbers in the textbox and i am trying to show commas only in the front end .



so when i go upto 9999 it gives correct output like 9,999 and when i type 10000 it gives output as 1,0,000


pls guide me

What I have tried:

this is my code 

    <pre><script type="text/javascript">

        function addCommas(nStr) {
            nStr += '';
            x = nStr.split('.');
            x1 = x[0];
            x2 = x.length > 1 ? '.' + x[1] : '';
            var rgx = /(^\d{2})|(\d{1,3})(?=\d{1,3}|$)/g;
            while (rgx.test(x1)) {
                x1 = x1.replace(rgx, '$1' + ',' + '$2');
            }
            return x1 + x2;
        }


    </script>



and this my html code line

<asp:TextBox ID="txtbudamt" runat="server" CssClass="text_box" Height="22px" Width="140px" onkeyup="this.value=addCommas(this.value);"
                                                                            onkeydown="return (event.keyCode!=13);" AutoComplete="Off" TabIndex="7"></asp:TextBox>
Posted
Updated 28-Sep-20 20:55pm
v2

See here: Number formatting in JavaScript - Tom's Blog[^]

But in future, please tag your questions correctly - this is not a C# problem, it's a javascript problem. Getting the tag wrong as you have means that people who know javascript but not C# will ignore your question and that can delay or prevent you getting a solution.
 
Share this answer
 
Comments
Member 13998042 29-Sep-20 2:29am    
thanks i will change it
Using Regex for formatting an integer is a little bit over engineered.
If you don't want to use native JS formatting feature, you can program it.
Algorithm:
JavaScript
function addCommas(nStr) {
  result= ""
  while (nStr.length > 3) {
    tail= the last 3 digits of nStr
    result= ","+ tail + result
    nstr= the first (length-3) digits of nstr
  }
  result= nstr + result
}

I let you search exact functions as exercise.
 
Share this answer
 

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