I have a numeric type input, which should only receive numbers (obvious) .
However, some browsers let the user type alphanumeric characters.
But the problem is not just that.
I need to display 0 on the left as long as the input value has not reached the limit of 4 characters disregarded the leading zeros.
For example: the initial value of the field is 0000 and the uzuario type 1 must be 0001 .
I have tried with Jquery Mask, but it only limits the data type and number of characters, but it does not do zeros on the left.
$(document).ready(function () {
//$('#client-number').mask('0000', {placeholder: '0000'});
/*Esse foi o melhor script que consegui fazer, porem ele não permite backspace, delete nem arrows, o que torna a experiência ruim*/
$('#client-number').keydown(function (evt) {
var fieldVal = $(this).val();
var key = evt.keyCode;
var char = String.fromCharCode((96 <= key && key <= 105)? key-48 : key);
if (!isNaN(char)) {
fieldVal = $(this).val() + char;
}
if (fieldVal < 9999 && evt.which != 32) {
$(this).val(paddy(fieldVal, 4));
return false;
} else {
return false;
}
});
function paddy(n, p, c) {
var pad_char = typeof c !== 'undefined' ? c : '0';
var pad = new Array(1 + p).join(pad_char);
return (pad + n).slice(-pad.length);
}
});
<script src="https://github.com/igorescobar/jQuery-Mask-Plugin/blob/master/dist/jquery.mask.min.js"></script><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="client-number" class="form-control" autofocus pattern="[0-9]*" inputmode="numeric" min="0" max="9999"
name="number" />