JSFiddle - React, Tailwind, and code Playground

by georgecalm

HTML

<input id="txt" type="text" name="txtChar" />

JavaScript

var isDigitOrDot = function(str) {
    return (str.match(/[^\d^.]/) === null);
};

var isStrValid = function(str) {
  return (isDigitOrDot(str) 
       && (str.replace(/\d+\.?\d?\d?/, "") === ""));
};

var node = dojo.byId("txt");
dojo.connect(node, "onkeyup", function() {
    if (!isStrValid(node.value)) {
      node.value = node.value.substring(0, node.value.length-1);
    }
});