Javascript Project to Add Commas to HTML5 Input Number Fields on Typing Using JQuery in Browser

You are currently viewing Javascript Project to Add Commas to HTML5 Input Number Fields on Typing Using JQuery in Browser

Javascript Project to Add Commas to HTML5 Input Number Fields on Typing Using jQuery in Browser

<h1>Add comma for input number automatically</h1>
<input type="text">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
  function updateTextView(_obj){
  var num = getNumber(_obj.val());
  if(num==0){
    _obj.val('');
  }else{
    _obj.val(num.toLocaleString());
  }
}
function getNumber(_str){
  var arr = _str.split('');
  var out = new Array();
  for(var cnt=0;cnt<arr.length;cnt++){
    if(isNaN(arr[cnt])==false){
      out.push(arr[cnt]);
    }
  }
  return Number(out.join(''));
}
$(document).ready(function(){
  $('input[type=text]').on('keyup',function(){
    updateTextView($(this));
  });
});
</script>

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply