Results 1 to 5 of 5

Thread: comma deilimited number

  1. #1
    Join Date
    Sep 2013

    comma deilimited number

    i want, when i type a number in text box to format the number,
    so i will get :
    12345678 ==>12,345,678
    i tried to use this code : http://blog.stevenlevithan.com/archives/commafy-numbers
    to format number when using the keyup event.
    (i tried to use code from other sources too but i get the same problem):
    when i copy paste the number the code works great and the number is formated as excpected,
    but when i try i get : 1,234,5,678

    what am i missing?
    this is one of my code test :
    function FixNumberRepresentation(obj, val) {
                var str = val;
                str = str.replace(",", "");
                str = str.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
                obj.value = str;
                return false;
    NET CODE :
    <asp:TextBox runat="server" ID="PrepaidStatus_action" Width="80px" MaxLength="11"
                                                                                                onkeyup="return FixNumberRepresentation(this,this.value)" />
                                                                                            <ajaxToolkit:FilteredTextBoxExtender ID="FilteredTextBoxExtender_PrepaidStatus_action"
                                                                                                runat="server" TargetControlID="PrepaidStatus_action" FilterType="Custom,Numbers"
                                                                                                ValidChars="," Enabled="True" />

  2. #2
    Join Date
    Oct 2010
    Versailles, France
    The onkeyup event can't be called many times with this function !
    After the first change, it is necessary to remove at first the commas. Change the function to remove it at first with a str=val.replace(/,/g,'').
    Last edited by 007Julien; 09-01-2013 at 06:39 AM.

  3. #3
    Join Date
    Sep 2013
    i do :
    str = str.replace(",", "");

  4. #4
    Join Date
    Oct 2010
    Versailles, France
    Sorry ! You are right.

    I do not understand the proposed asp code with a run at server ?

    Could you please post the result HTML page (or an extract).

    Then I proposed (See this page)
    str = str.replace(/(\d)(?=(\d{3})+\b)/g,'$1,');
    the \b is a word boundary which works with the end of the string and other commas, points, spaces... etc.

    It's to possible to call the function on the client side with a onblur event...
    Last edited by 007Julien; 09-01-2013 at 04:10 PM.

  5. #5
    Join Date
    Feb 2006
    You can use the comma insertion method here, but it is annoying to insert things while the user is typing.

    Better to use it once- onchange, or just before submitting, whenever you validate the input.

    <!doctype html>
    <html lang="en">
    <meta charset= "utf-8">
    <title>Comma Input</title>
    label, input, button{font-size:1.25em}
    // insert commas as thousands separators 
    function addCommas(n){
        var rx=  /(\d+)(\d{3})/;
        return String(n).replace(/^\d+/, function(w){
                w= w.replace(rx, '$1,$2');
            return w;
    // return integers and decimal numbers from input
    function validDigits(n){
        var n2= n.replace(/[^\d\.]+/g, '');
        var ax2= n2.lastIndexOf('.');
        if(ax2> n2.indexOf('.')) n2= n2.substring(0, ax2);
        return n2;
    window.onload= function(){
        var inp= document.getElementById('number1');
        inp.value= '';
        inp.onkeyup= inp.onchange= function(e){
            e=e|| window.event;    
            var who=e.target || e.srcElement,
            temp= validDigits(who.value);
            who.value= addCommas(temp);
    <h1>Input Commas</h1>
    <label> Any number <input id="number1" value="" size="30"></label>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center

Recent Articles