As I mentioned in my title I am trying to validate the user entered mobile number is a indian number or not .
For that I have referred a pattern from this regular expression for Indian mobile numbers
But in my case it always returns false.
I want to validate the following .
- number should starts with 6,7,8,9
- must have 10 digits
app.component.html
<form class="example-form" [formGroup]="userAddressValidations">
<mat-form-field appearance="outline" class="col-sm-6">
<mat-label>10-digit Mobile Number</mat-label>
<input matInput formControlName="mobileNumber" maxlength="10" (keypress)=_keyPress($event)>
<mat-error *ngIf="userAddressValidations.get('mobileNumber').hasError('required')">
Please fill out this field.
</mat-error>
<mat-error *ngIf="userAddressValidations.get('mobileNumber').hasError('pattern')">
It is not a valid mobile number.
</mat-error>
</mat-form-field>
</form>
app.component.ts
export class InputErrorStateMatcherExample {
userAddressValidations: FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.userAddressValidations = this.formBuilder.group({
mobileNumber: ['', [Validators.required, Validators.pattern('^[6-9]\d{9}$')]]
});
}
_keyPress(event: any) {
const pattern = /[0-9]/;
let inputChar = String.fromCharCode(event.charCode);
if (!pattern.test(inputChar)) {
event.preventDefault();
}
}
}
can anyone help me to fix it.