Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Angular 15 Custom Form Validation Example

Angular 15 Custom Form Validation Example

Posted on February 22, 2023June 21, 2025 By XpertPhp

In this tutorial, we will explain to you how to create custom form validation in angular 15(Angular 15 Custom Form Validation Example). In this example, we will use the reactive forms module for customized validation.

Create new angular app

We will create a new angular app using the following command.

1
2
ng new myapp
cd myapp

Import module in angular

In this step, we need to add a module into the src/app/app.module.ts file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
 
@NgModule({
 
  declarations: [
    AppComponent
  ],
 
  imports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule
 
  ],
  providers: [],
  bootstrap: [AppComponent]
 
})
 
export class AppModule { }

Update view file in angular

In this step, we have created a simple form with some fields. when we click on the submit button at that time check the validation. if validation is false then will be shown an error message.

src/app/app.component.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<div class="container">
  <h2>Angular 15 Custom Form Validation Example</h2>
<form [formGroup]="form" (ngSubmit)="submit()">
<div class="form-group">
<label for="email">Email</label>
<input formControlName="email" id="email" type="email" class="form-control">
<div *ngIf="validCheck.email.touched && validCheck.email.invalid" class="alert alert-danger">
<div *ngIf="validCheck.email.errors.required">Email is required.</div>
<div *ngIf="validCheck.email.errors.email">Please enter a valid email.</div>
</div>
</div>
<div class="form-group">
<label for="password">Password</label>
<input formControlName="password" id="password" type="password" class="form-control">
<div *ngIf="validCheck.password.touched && validCheck.password.invalid" class="alert alert-danger">
<div *ngIf="validCheck.password.errors.required">Password is required.</div>
<div *ngIf="validCheck.password.errors.minlength">Password must be at least 6 characters</div>
</div>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
</div>
See also  Angular 11 Bootstrap 4 Datepicker Example

Update component in angular

src/app/app.component.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { Component } from '@angular/core';
import { FormGroup, FormControl, Validators} from '@angular/forms';
 
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
 
})
 
export class AppComponent {
 
  form = new FormGroup({
    email: new FormControl('', [Validators.required, Validators.email]),
    password: new FormControl('',[ Validators.required, , Validators.minLength(6)])
  });
  
  get validCheck(){
    return this.form.controls;
  }
  
  submit(){
    console.log(this.form.value);
  }
}

Run Our Angular Application

Now we will run our Angular application using the below command.

1
ng serve

Angular

Post navigation

Previous Post: Laravel 10 Send Email Example Tutorial
Next Post: Laravel 10 Form Validation Example Tutorial

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Angular 15 CRUD Application Example Tutorial
  • Laravel 10 Form Validation Example Tutorial
  • Angular 15 Custom Form Validation Example
  • Laravel 10 Send Email Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com