I have this directive:
This directive should disable typing on input type="number"
after reaching max length (that passed as an input).
Because maxlength attribute doesn't work on type="number"
.
export class AIGMaxLengthDirectiveCO {
constructor(private elm: ElementRef) { }
@Input('aigmaxlengthCO') aigmaxlength: number;
@HostListener('keydown', ['$event'])
handleKeyboardEvent(e: KeyboardEvent) {
if (e.keyCode === 8 || e.keyCode === 9 || e.keyCode === 46) { // Backspace\tab\delete
return true;
} else if (this.isTextSelected(e)) {
return true;
} else if ((e.target as HTMLInputElement).value.length >= this.aigmaxlength) {
e.preventDefault();
return false;
}
}
isTextSelected(e) {
if (document.getSelection().toString() !== '') {
return true;
}
const textbox = e.target as HTMLInputElement;
const startIndex = textbox.selectionStart;
const endIndex = textbox.selectionEnd;
return endIndex - startIndex > 0;
}
}
<input [aigmaxlengthCO]="9" class="aig-input aig-17 input-text padding-0" type="number" formControlName="id" [ngClass]="form.get('id').invalid && (form.get('id').touched|| validations) ? 'error' : ''"/>
As in the third if
block you can see the
return false
and the preventDefault()
.
my problem that is on mobile Chrome it just doesn't stop the event, I can see that I enter the third "if" block, and should return false, but it's not working. On the desktop, all works fine
Things I tried:
- changing
keydown
tokeypress
. - changing
keydown
todocument:keydown
.