I want to define quantity of symbols in my regex
_keyPress(ptrn: string, event: any) {
const pattern = new RegExp(ptrn);
const inputChar = String.fromCharCode(!event.charCode ? event.which : event.charCode);
const value = event.target.value;
if (!pattern.test(inputChar) && !pattern.test(value)) {
event.preventDefault();
return false;
}
}
html:
<input (keypress)="_keyPress('[a-z]{,6}', $event)">
But it doesn't work if I use quantity quantifier. Who can say what's going wrong? https://plnkr.co/edit/fIVAvRJcubzD2SxZqlTY?p=preview
0as the minimum value -[a-z]{0,6}. Better with anchors to require the full string match -^[a-z]{0,6}$