I am trying to develop a contact form, I want user to enter phone number values between length 10-12.
Notably same validation is working on Message
You should not use length here, for min and max use custom validator like this,
var numberControl = new FormControl("", CustomValidators.number({min: 10000000000, max: 999999999999 }))
Angular2 min/max validators
I have a trick that 100% work.
Define input of type 'text' and not 'number'.
For eg:
<input placeholder="OTP" formControlName="OtpUserInput" type="text">
Then use pattern which is part of Validation.
Like :
this.ValidOtpForm = this.formbuilder.group({
OtpUserInput: new FormControl(
{ value:'', disabled: false },
[
Validators.required,
**Validators.minLength(6),
Validators.pattern('[0-9]*')**
]),
});
It means we define input type text that is suitable for min length and we also define pattern(validation) for numeric value so that we can achieve both validation.
Remaining code :
<mat-error *ngIf="RegistrationForm.controls['Password'].hasError('minlength')">Use 6 or more characters with a mix of letters</mat-error>
<mat-error *ngIf="ValidOtpForm.controls['OtpUserInput'].hasError('pattern')">Please enter numeric value.</mat-error>
If you want to validate a field by multiple validators then, You should try this
phone: ['', Validators.compose([
Validators.required,
Validators.minLength(10),
Validators.maxLength(12)])
])],
<div nz-col [nzXs]="24" [nzSm]="12" nz-form-control nzHasFeedback>
<nz-input formControlName="password" [nzPlaceHolder]="'password'" [nzType]="'password'" [nzSize]="'large'" (ngModelChange)="validateConfirmPassword()">
</nz-input>
<div nz-form-explain *ngIf="getFormControl('password').dirty&&getFormControl('password').hasError('minlength')">Your password must be at least 5 characters long. </div>
<div nz-form-explain *ngIf="getFormControl('password').dirty&&getFormControl('password').hasError('maxlength')">Your password cannot exceed 15 characters. </div>
<div nz-form-explain *ngIf="getFormControl('password').dirty&&getFormControl('password').hasError('required')">Please input your password!</div>
</div>
For a number
field, you can validate min and max values using built in Angular validation, like this:
.ts
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
private myNumberFieldMin: number = 1;
private myNumberFieldMax: number = 1000000;
constructor() {
this.myForm = this.formBuilder.group({
myNumberField
})
this.myForm.controls.myNumberField.setValidators([
Validators.min(this.myNumberFieldMin),
Validators.max(this.myNumberFieldMax)
]);
html
<form [formGroup]="myForm">
<input type="number" formControlName="myNumberField">
<div *ngIf="this.myForm.controls['myNumberField'].errors && this.myForm.controls['myNumberField'].errors.min">
<span class="error-message">Value must be at least {{myNumberFieldMin}}</span>
</div>
<div *ngIf="this.myForm.controls['myNumberField'].errors && this.myForm.controls['myNumberField'].errors.max">
<span class="error-message">Maximum value is {{myNumberFieldMax}}</span>
</div>
</form>
You can use a pattern instead to validate your phone number, and change your input type to text
<input type="text" formControlName="phone" placeholder="Phone Number">
and use this pattern for phone number tha have a length between 10 and 12
phonePattern = /^[0-9]{10,12}$/;
and change the validator on your form control
phone: ['', [Validators.required, Validators.pattern(this.phonePattern)]],
and you can display the error:
<div *ngIf="myForm.get('phone').invalid">
<div class="error-message invalid-feedback" *ngIf="myForm.get('phone').hasError('required')">
required!
</div>
<div class="error-message invalid-feedback" *ngIf="myForm.get('phone').hasError('pattern')">
invalid!
</div>
</div>