Добрый день,
У меня есть форма, где пользователи должны ввести сумму депозита. Я форматирую поле, чтобы показывать 2 десятичных знака, когда пользователь вводит числа следующим скриптом:
amountValue = "";
$(function() {
$(".mib").unbind().keydown(function(e) {
//handle backspace key
if (e.keyCode == 8 && amountValue.length > 0) {
amountValue = amountValue.slice(0, amountValue.length - 1); //remove last digit
$(this).val(formatNumber(amountValue));
} else {
var key = getKeyValue(e.keyCode);
if (key) {
amountValue += key; //add actual digit to the input string
$(this).val(formatNumber(amountValue)); //format input string and set the input box value to it
}
}
return false;
});
function getKeyValue(keyCode) {
if (keyCode > 57) { //also check for numpad keys
keyCode -= 48;
}
if (keyCode >= 48 && keyCode <= 57) {
return String.fromCharCode(keyCode);
}
}
function formatNumber(input) {
if (isNaN(parseFloat(input))) {
return "0.00"; //if the input is invalid just set the value to 0.00
}
var num = parseFloat(input);
return (num / 100).toFixed(2); //move the decimal up to places return a X.00 format
}
});
Мне нужно ограничить количество символов в поле до 9. Однако скрипт игнорирует мой html maxlength = "9":
input type="tel" id="amount" name="amount" class="full mib" pattern="\d+(\.\d{2})?$" title="Please enter a valid number">
Подробнее см. Мою скрипку .
Я пытался решить эту проблему, в том числе
$('input[name=amount]').attr('maxlength',9);
и
$('#amount').attr('maxlength',9);
Должно быть простое решение, которое я упустил.