add comma in number automatically in Javascript

Something Like that:
add comma in number automatically in Javascript

add this HTML code.

HTML

<input type="text" class="number">

add this JS code.

JS

var inputKeys = document.querySelectorAll('input.number');

for(i=0; i<inputKeys.length; i++){
	inputKeys[i].onkeyup = function(){
			var currentValue = this.value.split(',').join('');
			console.log('current:-' + currentValue);
			this.value = currentValue;
			var newVal = this.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
			console.log('new:-' + newVal);
			this.value = newVal;
    }
}

[codepen_embed height=”265″ theme_id=”0″ slug_hash=”wmaRjG” default_tab=”js,result” user=”pradeepanvi”]See the Pen add comma in number automatically in Javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]

You can check console.

Leave a Reply

Your email address will not be published. Required fields are marked *