1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Angular regex validator allows empty value, while the regex clause should demand at least one...

Discussão em 'Angular' iniciado por Marc W, Outubro 9, 2024 às 13:32.

  1. Marc W

    Marc W Guest

    I am having this error and I wonder if I am doing something wrong here or it is the component not working. I am working in typescript, angular 17.

    In my Formgroup I am specifying this:

    limitsForm = new FormGroup({inputLimit: new FormControl('', [Validators.pattern('[0-9]+')])});


    Then if I leave the field empty, my form group is valid according to 'this.limitsForm.valid'. I explicitly have to add 'Validators.required' and then the form is invalid when you leave the input empty. But I think the regex '[0-9]+' indicates one or more numbers, so it should fail without the required validator. Now I am patching it like:

    limitsForm = new FormGroup({inputLimit: new FormControl('', [Validators.pattern('[0-9]+'), Validators.required])});


    But I think the 'Validators.required' should not be necessary! Is this wrong working of the angular component? Or is my understanding of regex not correct?

    Continue reading...

Compartilhe esta Página