You can do this with HTML
and a little bit of Javascript
.
Since you don't want to use number
type on input, then use a pattern to match the format that you want, in this case x.xx
. So a pattern like this one [\d]+([\.][\d]{2})
would apply.
var inpObj = document.getElementById("myinp");
inpObj.addEventListener('input', () => {
if (!inpObj.checkValidity()) {
console.log(inpObj.validationMessage);
} else {
console.log('you typed: ' + inpObj.value);
}
});
<input type="text" id="myinp" name="myinp" pattern="[\d]+([\.][\d]{2})">
Since this is a readonly
input, pattern
will not help, then 2-3 simple lines of JS
can help.
function myFunction() {
let inp = document.querySelector('#myinp')
inp.value = parseFloat(inp.value).toFixed(2);
}
<body onload="myFunction()">
<input type="text" id="myinp" name="myinp" value="23.3" readonly="readonly">
</body>