Angular 2: Number validation with Regex

Viewed 3222

I'm trying to validate the number field in IE 11.

<input type="number" name="amount" [(ngModel)]="amount" (keypress)="validateNum($event) />

And in typescript

private validateNum(event: KeyboardEvent): void {
    const pattern = /^\d{1,3}[.]\d{1,6}$/g;
    let inputChar = String.fromCharCode(event.charCode);
    if(!pattern.test(inputChar)) {
        event.preventDefault();
    }
}

Here I'm trying to restrict the input to 3 digits and 5 decimals. The problem I'm facing is, regEx is validating for each key pressed (Not validating after the 3 digits pressed) and it is not allowing to enter any number. How do I validate the number after the numbers reach the restriction? Or is it possible to validate without regEx in Angular2?? I'm new to Angular and need suggestion.

2 Answers
Related