JSFiddle - React, Tailwind, and code Playground

by steveukx

HTML

<input type="number" id="foo" />

JavaScript

document.getElementById('foo').addEventListener('keypress', function(e) {
    if(!String.fromCharCode(e.which).match(/[0-9$\.,]/)) {
      console.log(e.charCode, e.keyCode, e.which);
      e.preventDefault();
    }
}, false);
    
document.getElementById('foo').addEventListener('blur', function(e) {
    alert('$' + parseFloat(this.value.replace(/[^0-9\.]g/, '')));
}, false);