JSFiddle - React, Tailwind, and code Playground

by Tan

HTML

<input id="amount" type="tel" name="amount" value="" onkeyup="this.value = numFormat(this.value)"/>


<div id="result">
0
</div>

<input type="button" value="strip it" onclick="go();">

JavaScript

function intFormat(n) {
     
     var regex = /(\d)((\d{3},?)+)$/;
     n = n.split(',').join('');
     while(regex.test(n))
        {
          n = n.replace(regex, '$1,$2');
        }
        n = n.replace("£", "");
        return '£' + n;
      }
      
function numFormat(n) {
       
     var pointReg = /([\d,\.]*)\.(\d*)$/, f;
     if(pointReg.test(n))
        {
          f = RegExp.$2;
          return intFormat(RegExp.$1) + '.' + f;
        }
        return intFormat(n);
      }	




function go() {

var res = document.getElementById("amount").value.replace(/[^0-9]/g, "");     
document.getElementById("result").innerHTML = res;

}