angularangular-validator

Do I need required if the pattern already implement this aspect?


I want some fields in a form to be required and to respect some pattern.

Currently I have this : Validators.required, Validators.pattern('^[0-9-_\/]+$')

but I was curious, is the .required is needed since the + in the regex already specify I need more than 0 element ? Is there anything the required validators does that a '.+' regex wouldn't already have ?


Solution

  • This is only for UI purposes.

    One will display the pattern error, the other one the required error.

    If you wish to display distinct errors, then you should keep it.

    Otherwise, if you simply display "Error on the form" (or something alike), then no, you don't need to use the required validator.