JSFiddle - React, Tailwind, and code Playground
HTML
<input id="txtChar" type="text" />
CSS
#txtChar:required:valid {
border: 1px solid green;
}
#txtChar:required:invalid {
border: 1px solid red;
}
JavaScript
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode == 46 || charCode > 31 && (charCode < 48 || charCode > 57)){
evt.preventDefault();
return false;
}
return true;
}
txtChar = document.getElementById("txtChar")
txtChar.addEventListener("keypress", isNumberKey, false);
//Attributes
txtChar.type = "number";
txtChar.min = 0;
txtChar.pattern = "\\d+";
txtChar.placeholder = "Only integer positive numbers";
txtChar.required = "required";