I am trying to only allow 9 digits in a single field without using JQuery. I am unable to modify html input. Thought this would be simple without JQuery but it doesn't seem that way. Any assistance would be appreciated.
Fiddle: https://jsfiddle.net/wj_fiddle_playground/b46tpaLj/4/
HTML Below:
<input class="formInput" type="text" style="width: 200px;" name="myNumber" id="myNumber" value="">
Javascript Below
function setInputFilter(textbox, inputFilter) {
["input", "keydown", "keyup", "mousedown", "mouseup", "select", "contextmenu",
"drop"].forEach(function(event) {
textbox.addEventListener(event, function() {
if (inputFilter(this.value)) {
this.oldValue = this.value;
this.oldSelectionStart = this.selectionStart;
this.oldSelectionEnd = this.selectionEnd;
} else if (this.hasOwnProperty("oldValue")) {
this.value = this.oldValue;
this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
} else {
this.value = "";
}
});
});
}
setInputFilter(document.getElementById("myNumber"), function(value) {
return /^\d{9}/.test(value); });