Example
JS
function limit(element)
{
var max_chars = 2;
if(element.value.length > max_chars) {
element.value = element.value.substr(0, max_chars);
}
}
HTML
<input type="number" onkeydown="limit(this);" onkeyup="limit(this);">
If you are using jQuery you can tidy up the JavaScript a little:
JS
var max_chars = 2;
$('#input').keydown( function(e){
if ($(this).val().length >= max_chars) {
$(this).val($(this).val().substr(0, max_chars));
}
});
$('#input').keyup( function(e){
if ($(this).val().length >= max_chars) {
$(this).val($(this).val().substr(0, max_chars));
}
});
HTML
<input type="number" id="input">
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…