JSFiddle - React, Tailwind, and code Playground

by moob

HTML

<input id="aninput" type="text" onkeypress="return validateFloatKeyPress(this,event);" />

JavaScript

function validateFloatKeyPress(el, evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    var number = el.value.split('.');
    if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    if(el.value.indexOf(".")>-1 && (number.length == 2 && number[1].length > 1)){
        return false;
    }    
    return true;
}