10 Digit Mobile Number Validation in Angular 13

10 digit phone/mobile number validation in angular 13; Through this tutorial, i am going to show you how to validate 10 digit phone/mobile numbers in the angular 13 apps.

10 Digit Mobile/Phone Number Validation in Angular 13

Use the below given steps to validate 10 digit phone/mobile number in angular 13 apps; as follows:

  • Step 1 – Create New Angular App
  • Step 2 – Install Bootstrap Library
  • Step 3 – Import Modules
  • Step 4 – Create Form in View File
  • Step 5 – Add Code On app.Component ts File
  • Step 6 – Start the Angular App

Step 1 – Create New Angular App

Run the following command on it terminal to install angular app:

ng new my-new-app

Step 2 – Install Bootstrap Library

Then run the following command on your terminal to install bootstrap library in angular app. So, You can install the packages by executing the following commands on the terminal:

npm install --save bootstrap

Then you need to add below code into your angular.json file:

...
"styles": [
              "src/styles.css",
              "node_modules/bootstrap/dist/css/bootstrap.min.css"
          ],
"scripts": ["node_modules/bootstrap/dist/js/bootstrap.min.js"]
...

Step 3 – Import Modules

Go to src/app directory and open app.module.ts file. And then add the following lines of into app.module.ts file:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    ReactiveFormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Step 4 – Create Form in View File

Create form for accept a 10 digit phone or mobile number in the angular app. Go to src/app/ directory and open app.component.html and update the following code into it:

<form [formGroup]="registerForm" (ngSubmit)="onSubmit()">
<div class="col-md-4">
                    <div class="form-group">
                        <label for="">YOUR PHONE NUMBER </label>
                        <input (keypress)="keyPress($event)" required type="text" formControlName="phonenumber" class="form-control" placeholder="Enter Your phone Number" [ngClass]="{ 'is-invalid': submitted && f.phonenumber.errors }">
                        <div *ngIf="submitted && f.phonenumber.errors" class="invalid-feedback">
                        <div *ngIf="f.phonenumber.errors.required">Phone number is required</div>
                        <div *ngIf="f.phonenumber.errors.pattern || f.phonenumber.errors.maxlength || f.phonenumber.errors.minlength">Phone number must be at least 10 numbers</div>
                    </div>
                    </div>
                </div>
<input type="submit" class="mw-ui-btn" value="Submit">
</form>

Step 5 – Add Code On app.Component ts File

Go to the src/app directory and open app.component.ts. Then add the following code into component.ts file:

import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  registerForm: FormGroup;
  submitted = false;
  constructor(private formBuilder: FormBuilder) { }
  //only number will be add
  keyPress(event: any) {
    const pattern = /[0-9\+\-\ ]/;
    let inputChar = String.fromCharCode(event.charCode);
    if (event.keyCode != 8 && !pattern.test(inputChar)) {
      event.preventDefault();
    }
  }
  ngOnInit() {
    this.registerForm = this.formBuilder.group({
       phonenumber: ['', [ Validators.required,
        Validators.pattern("^[0-9]*$"),
        Validators.minLength(10), Validators.maxLength(10)]]
    });
}
// convenience getter for easy access to form fields
get f() { return this.registerForm.controls; }
onSubmit() {
    this.submitted = true;
    // stop here if form is invalid
    if (this.registerForm.invalid) {
        return;
    }
   
}
  
}

Step 6 – Start the Angular App

Run the following command on terminal to start angular phone number validation app:

ng serve

Conclusion

10 digit phone/mobile number validation in angular 13; Through this tutorial, You have learned how to validate 10 digit phone/mobile numbers in the angular 13 apps.

Be First to Comment

Leave a Reply

Your email address will not be published.