Using the pattern attribute do not prevent entering anything inside the input field. It will just mark it as invalid.
You must use a Directive to do so.
Here is a simple example that prevent entering anything but positive number into a input field:
import {Directive, HostListener} from '@angular/core';
@Directive({
selector: 'input[dsancNumeroPositif]'
})
export class NumeroPositifDirective {
acceptedCharacters: string[] = ['.', ',', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
constructor() { }
@HostListener('keypress', ['$event']) onInput(event: KeyboardEvent): void
{
if (!this.acceptedCharacters.includes(event.key)) {
event.preventDefault();
}
}
}