JSFiddle - React, Tailwind, and code Playground

by huuzoo

HTML

<input type="text" id="form" onchange="addComma()">
<div>金額:<span id="price"></span></div>

JavaScript

function addComma() {
	var form = document.getElementById('form');
	var form_value = form.value;
  if (form_value.match(/[0-9]+/g) != form_value ) {
		document.getElementById('price').innerHTML = '数値以外はNGです。';
    form.value = '';
  } else {
		var commaNum = String( form_value ).replace( /(\d)(?=(\d\d\d)+(?!\d))/g, '$1,' );
  	document.getElementById('price').innerHTML = commaNum;
    form.value = '';  
  }
}