Explore Angular 4 Form Validation With Example – For Beginners


This article will guide you to over Angular 4 form validation with example on both model and reactive forms approach.

Angular 4 Form Validation With Example

As we’ve already gone through on Forms tutorial, there’re two kinds of forms approach in Angular 4. One is template driven and another is reactive forms approach.

Template-driven, as name suggested, it’s completely dependent on the HTML part and there’s no dependency on the component or model.

Where as, Reactive approach (Model-driven), is completely dependent on the component and it passes the validations to the HTML. Let’s see how they both works.

First of all, we’ll create a template-driven form and then we’ll go for reactive approach.

1. Angular 4 Template driven forms validation

For template driven forms validation, you can use all the methods which we can use for native HTML forms.

Whenever the value of form control changes, Angular runs validations and results in a state of validations, i.e. either INVALID or VALID.

Let’s take an example.

This following element will have the validation required, minLength and forbiddenName.



So, whenever you’d like to show a message for a particular element, you can use methods like invalid, dirty and touched.


Name is required.

Name must be at least 4 characters long.

Name cannot be Bob.



  • As mentioned, you can apply native HTML validation methods like required, minLength, etc.
  • #name=”ngModel”, will exports NgModel into a local variable called name. This way you can find the state is valid or dirty.
  • Finally, to show the respective validation methods, you can use ngIf directive with form control and it’s validation methods.

2. Angular 4 Reactive forms validation

Reactive forms are completely driven by components and validations will be defined in the component itself.

Let’s define a simple form and it’s validations


//Import core modules
import { Component, OnInit } from ‘@angular/core’;
//Import forms modules
import { FormControl, FormGroup, Validators } from ‘@angular/forms’;

//Component decorator
selector: ‘app-root’,
templateUrl: ‘./app.component.html’,
styleUrls: [‘./app.component.css’]
//App component class
export class AppComponent implements OnInit {
title = ‘Angular 4 Reactive Forms’;
simpleForm: FormGroup;

ngOnInit() {
this.simpleForm = new FormGroup({
‘name’: new FormControl(null, Validators.required),
‘desc’: new FormControl(null, Validators.required),

//Save data code


  • First part, importing the necessary module into the component.
  • The middle part has the @Component decorator.
  • In the last part, class has defined the simpleForm as a FormGroup with number of members i.e. FormControl.

In reactive forms, this way you can define the form group and their form-controls for structuring the forms and it’s validations.

Now, let’s see how we can implement the actual HTML form in the template.


Here, we’ll defined a simple HTML form with the FormGroup and FormControl.


Please enter name.

Please enter desc.



  • Firstly, we’ve defined the form with formGroup named as simpleForm that we’ve actually defined in the component.
  • Secondly, we’ve two form controls as formControlName which we’ve also we’ve defined in our component as FormControl.
  • Third is we can show the validations by using the default methods like valid and touched.

So, when the fields are empty and fields been touched by the user then these validations will be triggered and will show the defined messaged respectively.

There’re number of validation states available that you can use to provide the CSS classes.

Also, you can make the submit button disabled till your form is fully valid.



Suggested visit: Build your first form using Reactive Forms

I hope, you now have an idea of using Angular 4 form validation with example for both the approaches.

0 0 votes
Article Rating
Notify of
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
4 years ago

Thanks a lot! Clearly and briefly.

classy ebony stepdaughter screwed sensually.kirmiziyilan.com

Would love your thoughts, please comment.x