-1

My webpage has an input textbox where a user must enter a number (e.g. "100000"). When the textbox loses focus, I'd like to format this data into a human-readable format, like "100,000". Is it possible to do this in javascript?

Adam Link
  • 2,783
  • 4
  • 30
  • 44
user1029388
  • 9
  • 1
  • 3
  • Talk a look at this question on how to format numbers in JavaScript, http://stackoverflow.com/questions/5731193/how-to-format-numbers-using-javascript – a'r Nov 04 '11 at 09:32

3 Answers3

2

here you have the example: http://jsfiddle.net/manuel/qgLn4/1/

function addCommas(nStr)
{
    nStr += '';
    x = nStr.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;
}
document.getElementById("a").onblur = function() {
    this.value = addCommas(this.value.replace(',', ''))
}
Manuel van Rijn
  • 10,170
  • 1
  • 29
  • 52
0

See if this plugin might help: http://plugins.jquery.com/project/maskinputmoney

I have used it, by the way :)

John Gathogo
  • 4,495
  • 3
  • 32
  • 48
0

Mostly Indian represent there number in his format.

document.getElementById("elem").onblur =function (){
    var val = this.value.replace(/\,/g,"").split("").reverse();
    if( val.length <= 3) return;
     var newval = [];
    for(var i = 0; i< val.length; i++){
        if( i == 3 || (i > 3 && (i - 3)%2 == 0 ) ){
            newval.push( ",");
        }

        newval.push( val[i]);       
    }
this.value = newval.reverse().join("");
}; 

jsfiddler

Anoop
  • 23,044
  • 10
  • 62
  • 76