Angular 2 form validation, minLength validator is not working
The error key is minlength
and not minLength
:
<div *ngIf = "password.hasError('minlength') && !password.hasError('required')" class = "alert alert-danger"> <span>Password should contain 6 characters</span></div>
This really caught me out as well as I matched the key in my markup to what I had in code which is incorrect.
Sample Code
password1: ['', [Validators.required, Validators.minLength(8)]],
Sample Markup
*ngIf="registrationRequest.get('password1').hasError('minlength')"
Note in the code it's minlength
entirely in lower case.
I was facing the same issue but after so many research I got a solution with this,Please use minlength insted of minLengthHere is the example.
<div *ngIf = "password.errors.minlength && !password.errors.required" class = "alert alert-danger"> <span>Password should contain 6 characters</span> </div>
Instead of
<div *ngIf = "password.errors.minLength && !password.errors.required" class = "alert alert-danger"> <span>Password should contain 6 characters</span></div>
Hope this will help someone, Thanks