JSFiddle - React, Tailwind, and code Playground
HTML
<input type="text" class="myTexbox" />
JavaScript
//Required: User can enter only two places after . or ,
//Cannot repeat comma or decimal once inserted. Both could not come together as both are interchangable
//Required: User can use paste i.e Ctrl+v but could not enter anything expect 0 - 9 with Single . or , with two places after it
//Required: Should work in IE 10 +
//Optional: If user enter some other button a message flash next to textbox
$(document).ready(function() {
$('.myTexbox').keypress(function (event) {
return isDecimalNumber(event, this)
});
});
function isDecimalNumber(eve, element) {
var charCode = (eve.which) ? eve.which : event.keyCode
if ((
(charCode != 8) //For Backspace Active
&&
(charCode != 44 || $(element).val().indexOf(',') != -1)
//For comma
&&
(charCode != 46 || $(element).val().indexOf('.') != -1) &&
(charCode < 48 || charCode > 57))
||
$(element).val().toString().match("\\.[0-9]{2,}$"))
return false;
return true;
}