Angular 14 URL validation using regular expression; In this tutorial, we will learn how to validate URL in angular 14 applications using regex.
Angular 14 Regex Validate URL with Reactive Forms Example
Use the following steps and validate url using regex in angular 14 apps; as follows:
- Step 1 – Create New Angular App
- Step 2 – Install Bootstrap Library
- Step 3 – Update Dependencies in Module.ts File
- 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
First of all, open your terminal and execute the following command on it to install angular app:
ng new my-new-app
Step 2 – Install Bootstrap Library
In this step, execute 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 – Update Dependencies in Module.ts File
In this step, visit src/app directory and open app.module.ts file. And then add the following lines of into app.module.ts file:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
ReactiveFormsModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Step 4 – Create Form in View File
In this step, create simple form for validating URL using regex in angular 14 app. So, visit src/app/ and app.component.html and update the following code into it:
<div class="container mt-5">
<h2>Angular 14 Regex URL Pattern Validation Example Tutorial - Tutsmake.com</h2>
<form [formGroup]="myForm" (ngSubmit)="onSubmit()" novalidate>
<div class="form-group">
<label>Enter URL</label>
<input type="text" formControlName="url" class="form-control mb-2">
<div *ngIf="m['url'].touched && m['url'].invalid" class="alert alert-danger">
<div *ngIf="m['url'].errors && m['url'].errors['required']">Please provide url</div>
<div *ngIf="m['url'].errors && m['url'].errors['pattern']">Please provide valid url</div>
</div>
</div>
<div class="d-grid mt-3">
<button class="btn btn-dark" [disabled]="!myForm.valid" type="submit">Store</button>
</div>
</form>
</div>
Step 5 – Add Code On app.Component ts File
In this step, visit 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.scss']
})
export class AppComponent {
public myForm: FormGroup;
constructor(private formBuilder: FormBuilder) {
this.myForm = formBuilder.group({
url: ['', [Validators.required, Validators.pattern('(https?://)?([\\da-z.-]+)\\.([a-z.]{2,6})[/\\w .-]*/?')]]
})
}
get m(){
return this.myForm.controls;
}
onSubmit(){
console.log(this.myForm.value);
}
}
Step 6 – Start the Angular App
In this step, execute the following command on terminal to start angular phone number validation app:
ng serve
Recommended Angular Tutorials