my code as below:
html
<form [formGroup]="demo2Group">
<input formControlName="demo2Value">
<div *ngIf="demo2Value.invalid && (demo2Value.dirty || demo2Value.touched)">
<div *ngIf="demo2Value.errors.required">
the value can't be empty
</div>
<div *ngIf="demo2Value.errors.minlength">
the value can't less than 4 length
</div>
<div *ngIf="demo2Value.errors.maxLength">
the value can't more than 6 length
</div>
</div>
</form>
ts
demo2Group;
ngOnInit() {
this.demo2Group = new FormGroup({
'demo2Value': new FormControl(null, [
Validators.required,
Validators.minLength(4),
Validators.maxLength(6),
])
});
}
get demo2Value() { return this.demo2Group.get('demo2Value'); }
The required and minlength works,but the maxLength didn't work.If I use template validation.The input element can't input more than 6.
But now,I can input more than 6 and didn't show any error.