Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 - Validators pattern not working

Tags:

angular

I am using a reactive form in angular 6, and trying to do some validation on a password field. Basically it needs to be more than 8 characters, contain at least 1 upper case, and contain at least 1 symbol.

my regex which I have tested, is ^(?=.*[A-Z])(?=.*[!@#\$%\^&\*])(?=.{9,})

Here is an excerpt from my login component ts:

  ngOnInit() {
        this.loginForm = this.formBuilder.group({
            userName: ['Username', [Validators.required]],
            password: ['Password', [Validators.pattern("^(?=.*[A-Z])(?=.*[!@#\$%\^&\*])(?=.{9,})")]]            
        });
    }

Here is my form group div:

 <div class="form-group">
                      <input type="password" formControlName="password" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.password.errors }" />
                      <div *ngIf="submitted && f.password.errors" class="invalid-feedback">              
                          <div *ngIf="f.password.errors.pattern">Password must contain more than 8 characters, 1 upper case letter, and 1 special character</div>
                      </div>
                  </div>

Basically the password does not validate even if it appears to be correct. Any tips would be appreciated.

like image 946
sm1l3y Avatar asked Sep 28 '18 19:09

sm1l3y


People also ask

How to set pattern validation in Angular?

Pattern Validation with formControl username = new FormControl(); In HTML template we will use Angular pattern attribute with regex in following way. We can bind component property with pattern using Angular property binding. Suppose we have following component property for regex to validate user name.

What is ValidatorFn in Angular?

ValidatorFnlinkA function that receives a control and synchronously returns a map of validation errors if present, otherwise null. interface ValidatorFn { (control: AbstractControl<any, any>): ValidationErrors | null }

What is regex in Angular?

The ng-pattern Directive in AngularJS is used to add up pattern (regex pattern) validator to ngModel on input HTML element. It is used to set the pattern validation error key if input field data does not match a RegExp that is found by evaluating the Angular expression specified in the ngpattern attribute value.

How do I remove required validator?

Either first use “clearValidators()” to remove all validators and then use “setValidators()” to set needed validation. Or directly use “setValidators()” with the needed validators only (without the validator you don't want to have).


2 Answers

ng-pattern seem to take a regExp as parameter (like in angularJs) :

Validators.pattern(new RegExp("^(?=.*[A-Z])(?=.*[!@#\$%\^&\*])(?=.{9,}))")

You can also use /..../

Validators.pattern(/^(?=.*[A-Z])(?=.*[!@#\$%\^&\*])(?=.{9,})/)

Should work ;)

like image 189
xrobert35 Avatar answered Oct 31 '22 07:10

xrobert35


I think it's due to the *ngIf condition in your HTML.]

Try *ngIf="loginForm.controls.password.hasError('pattern')"

like image 37
T.Liu Avatar answered Oct 31 '22 06:10

T.Liu