The below function I have created takes input in decimal only and maximum 12 char are allowed in it but it failed in 1 case where if user enters for example 12.45 and he/she again puts the cursor behind the decimal and start typing, it doesn't let the user enter though the capacity is 9 char before decimal and 2 after the decimal.
The Function is called on the keypress event.
Any help is appreciated Thanks!!
function isDecimalNumberWithOnlyNumbersandScaleTwoFunc(event) {
if (!event)
event = window.event;
var charCode = (event.which) ? event.which : event.keyCode;
var elementValue = event.target.value;
if (event.key == "." && elementValue.indexOf('.') != -1) {
event.preventDefault();
return false;
}
if ((event.key == "." || isNumberKey(event))) {
if ((event.key == "." || isNumberKey(event)) && (elementValue.substring(elementValue.indexOf('.'), elementValue.length).length <= 2)) {
return true;
}
else if (isNumberKey(event)) {
if ((elementValue.indexOf('.') != -1)) {
event.preventDefault();
return false;
}
else if (elementValue.length < 9) {
if (isNumberKey(event) || event.key.toLowerCase() == "del" || event.key.toLowerCase() == "delete" || charCode == 46) {
return true;
}
else
return false;
}
}
else if (event.key == ".") {
return true;
}
event.preventDefault();
return false;
}
else
return false;
}