テキストフィールドを検証して有効な10進数を受け入れるための解決策を見つけるのを手伝ってくれる人はいますか? 私は何かを試しました
function fun_AllowOnlyAmountAndDot(txt) {
if (event.keyCode > 47 && event.keyCode < 58 || event.keyCode == 46) {
var txtbx = document.getElementById(txt);
var amount = document.getElementById(txt).value;
var present = 0;
var count = 0;
if (amount.indexOf(".", present) || amount.indexOf(".", present + 1)); {
// alert('0');
}
/*if(amount.length==2)
{
if(event.keyCode != 46)
return false;
}*/
do {
present = amount.indexOf(".", present);
if (present != -1) {
count++;
present++;
}
}
while (present != -1);
if (present == -1 && amount.length == 0 && event.keyCode == 46) {
event.keyCode = 0;
//alert("Wrong position of decimal point not allowed !!");
return false;
}
if (count >= 1 && event.keyCode == 46) {
event.keyCode = 0;
//alert("Only one decimal point is allowed !!");
return false;
}
if (count == 1) {
var lastdigits = amount.substring(amount.indexOf(".") + 1, amount.length);
if (lastdigits.length >= 4) {
//alert("Two decimal places only allowed");
event.keyCode = 0;
return false;
}
}
return true;
} else {
event.keyCode = 0;
//alert("Only Numbers with dot allowed !!");
return false;
}
}