Angular is smart and flexible framework to build single page applications, it provides two ways to work with forms, template drive form and Reactive form. Template driven form where we write our validations, controls and groups etc whereas Reactive form is smart and has all features in it by default.
Reactive form provides uses model driven approach while working with forms where you can easily handle inputs and binding. Template driven is smart but we need to write all logic for creating forms, here in reactive forms we get it all in.
I am going to show you how you can use form validation in angular there are many ways to show angular validation error message.
In this section I will demonstrate you how you can apply angular form validation on submit using angular form builder and angular formgroup.We are going to use reactive forms for form validation in angular for that we will use ReactiveFormsModule.
we need to add ReactiveFormsModule from angular form library that is '@angular/forms'.Now we are ready to use angular formGroup and angular form builder in our app. lets create a angular formgroup using angular form builder.
You may find the complete code at bottom of this article
lets import angular formGroup from angular forms library and take a new variable of type angular FormGroup and inject angular FormBuilder in constructor as private.For Angular validation error message we need to let angular validator know those all attributes we got to use in angular validation reactive form.
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'app-custom-validator',
templateUrl: './custom-validator.component.html',
styleUrls: ['./custom-validator.component.css']
})
export class CustomValidatorComponent {
form: FormGroup;
constructor(private formBuilder: FormBuilder) { }
}
First Name and Last Name:
firstName: ['', [Validators.required, Validators.pattern('^[a-zA-Z \-\']+')]]
Email Validation
email: ['', [Validators.required, Validators.email]],
Password and Confirm Password
ngOnInit(): void {
this.form = this.formBuilder.group({
password: ['', [Validators.required]],
confirmPassword: ['', [Validators.required]],
}, { validators: this.checkPasswords });
}
checkPasswords(group: FormGroup) { // here we have the 'passwords' group
const password = group.get('password').value;
const confirmPassword = group.get('confirmPassword').value;
return password === confirmPassword ? null : { notSame: true }
}
Gender using Radio input fields
gender: ['', [Validators.required]]
Phone Number
phoneNumber: ['', [Validators.required, Validators.minLength(11),
Validators.maxLength(11), Validators.pattern('^[0-9]*$')]]
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'app-custom-validator',
templateUrl: './custom-validator.component.html',
styleUrls: ['./custom-validator.component.css']
})
export class CustomValidatorComponent implements OnInit {
form: FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit(): void {
this.form = this.formBuilder.group({
firstName: ['', [Validators.required, Validators.pattern('^[a-zA-Z \-\']+')]],
lastName: ['', [Validators.required, Validators.pattern('^[a-zA-Z \-\']+')]],
email: ['', [Validators.required, Validators.email]],
password: ['', [Validators.required]],
confirmPassword: ['', [Validators.required]],
gender: ['', [Validators.required]],
phoneNumber: ['', [Validators.required, Validators.minLength(11),
Validators.maxLength(11), Validators.pattern('^[0-9]*$')]]
}, { validators: this.checkPasswords });
}
onSubmit() {
if (!this.form.valid) {
this.form.markAllAsTouched();
} else {
return true;
}
}
checkPasswords(group: FormGroup) { // here we have the 'passwords' group
const password = group.get('password').value;
const confirmPassword = group.get('confirmPassword').value;
return password === confirmPassword ? null : { notSame: true }
}
}
HTML
<div class="container">
<div class="row">
<div class="col text-center">
<h1 class="font-weight-bold">Angular Validations using Reactive form/ Custom validation</h1>
</div>
</div>
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>First Name</label>
<input required type="text" class="form-control" placeholder="Enter First Name"
formControlName='firstName'>
<small class="form-text text-danger"
*ngIf=" (form.controls['firstName'].errors?.required) && form.controls['firstName'].touched">Please
Enter your first Name</small>
<small class="form-text text-danger"
*ngIf="(form.controls['firstName'].errors?.pattern) && form.controls['firstName'].touched">First
name should only contain characters</small>
</div>
<div class="form-group">
<label>Last Name</label>
<input required type="text" class="form-control" placeholder="Enter Last Name"
formControlName='lastName'>
<small class="form-text text-danger"
*ngIf="(form.controls['lastName'].errors?.required) && form.controls['lastName'].touched">Please
Enter your last Name</small>
<small class="form-text text-danger"
*ngIf="(form.controls['lastName'].errors?.pattern) && form.controls['lastName'].touched">Last
name should only contain characters</small>
</div>
<div class="form-group">
<label>Email</label>
<input required type="email" class="form-control" placeholder="Enter your email"
formControlName='email'>
<small class="form-text text-danger"
*ngIf="(form.controls['email'].errors?.required) && form.controls['email'].touched">Please Enter
your email address</small>
<small class="form-text text-danger"
*ngIf="(form.controls['email'].errors?.email) && form.controls['email'].touched">Please Enter a
valid email address</small>
</div>
<div class="form-group">
<label>Password</label>
<input required type="password" class="form-control" placeholder="Enter your Password"
formControlName='password'>
<small class="form-text text-danger"
*ngIf="(form.controls['password'].errors?.required) && form.controls['password'].touched">Please
Enter a password</small>
</div>
<div class="form-group">
<label>Confirm Password</label>
<input required type="password" class="form-control" placeholder="Enter your confirm Password"
formControlName='confirmPassword'>
<small class="form-text text-danger"
*ngIf="(form.controls['confirmPassword'].errors?.required) && form.controls['confirmPassword'].touched">Please
confirm your password</small>
<small class="form-text text-danger"
*ngIf="form.hasError('notSame') && form.controls['confirmPassword'].touched">Please match your
password</small>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<input required type="radio" class="form-check-input" value="male" name="gender"
formControlName="gender">
<label class="form-check-label">Male</label>
</div>
<div class="col-md-6">
<input required type="radio" name="gender" value="female" class="form-check-input"
formControlName="gender">
<label class="form-check-label">Female</label>
</div>
</div>
<small class="form-text text-danger"
*ngIf="(form.controls['gender'].errors?.required) && form.controls['gender'].touched">Please
choose a gender</small>
</div>
<div class="form-group">
<label>Phone Number</label>
<input required type="text" class="form-control" placeholder="Enter your phone number"
formControlName="phoneNumber">
<small class="form-text text-danger"
*ngIf="(form.controls['phoneNumber'].errors?.required) && form.controls['phoneNumber'].touched">Please
Enter your phone number</small>
<small class="form-text text-danger"
*ngIf="(form.controls['phoneNumber'].errors?.minlength || form.controls['phoneNumber'].errors?.maxlength) && (form.controls['phoneNumber'].touched && !form.controls['phoneNumber'].errors?.pattern)">Phone
number should contain 11 digits</small>
<small class="form-text text-danger"
*ngIf="(form.controls['phoneNumber'].errors?.pattern) && form.controls['phoneNumber'].touched">Phone
number should only contain integers</small>
</div>
<div class="row">
<div class="col">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</div>
</form>
</div>
0 Comments