Introduction
In this article, we will learn about validations in the reactive form in Angular. We will create a simple user registration form and implement some inbuilt validations on it. Along with the inbuilt validations, we will also implement some custom validations to the reactive form. We will consider the following custom validations for this demo.
- Checking for user name availability
- Password pattern validation
- Match the password entered in two different fields
Take a look at the application in action.

Prerequisites
- Install Visual Studio code from https://code.visualstudio.com/
- Install the latest version of Angular CLI from https://cli.angular.io/
Source Code
Get the source code from GitHub.
Create the Angular app
Navigate to the folder where you want to create your project file. Open a command window and run the command shown below.
We are specifying the command to create a new Angular application. The option to create the routing module is set to false and style files extension is set to
scss. This command will create the Angular project with name as angular-forms-validation.
Change directory to the new project and open the project in VS Code using the set of the command shown below:
Install Bootstrap
Run the following command to install the Bootstrap library.
Add the following import definition in
styles.scss file.Create the validation service
Run the following command to create a new service.
This command will create a folder named services having two files inside it –
customvalidation.service.ts and customvalidation.service.spec.ts. Open the customvalidation.service.ts file and put the following code inside it.
The method
patternValidator is used to validate the password pattern in our form. The parameter for this method is of type AbstractControl which is a base class for the FormControl. We will use a regular expression to validate the password. We will validate the following four conditions using the regular expression:- The password should be a minimum of eight characters long.
- It has at least one lower case letter.
- It has at least one upper case letter.
- Contains at least one number.
If the password fails the regex check, we will set the
invalidPassword property to true.
The method
MatchPassword is used to compare the passwords in two fields. This method will accept two parameters of type string. These parameters represent the name of the fields to be matched. We will get the FormControl for these two fields and then match the values in them. If the values do not match, we will set the passwordMismatch property to true.
The method
userNameValidator is used to verify if the username is already taken or not. This method will accept a parameter of type AbstractControl. We will check if the value of this field is present in a static array, UserList. If the value entered by the user is already present, we will set the userNameNotAvailable property to true. We are using the setTimeout function to invoke this check every one second. This will ensure that the error will be thrown after one second from the time the user stops typing in the field.For the sake of simplicity of this article, we are using a static array to search for the availability of user names. Ideally, it should be a service call to the server to search the value in a database.
Create the reactive form component
Run the following command to create the reactive-form component.
Open
reactive-form.component.ts and put the following code in it.
We will create a variable
registerForm of type FormGroup. In the ngOnInit method, we will set the controls for the form using the FormBuilder class. All the fields are set as a required field for this form. We will invoke the userNameValidator method of the service using the bind function. For the password field, we will use the compose method to merge in multiple validators into a single function. We will also invoke the MatchPassword method and pass the name of the password and confirmPassword form controls as parameters.
The
registerFormControl property will return the form controls of the form. The onSubmit method will print the content of the form on the console if the form is valid and submitted successfully.
Open
reactive-form.component.html and put the following code in it.
We will create a reactive form and use the Bootstrap card for styling. The card header will contain a title whereas the card body will have the form fields. We will bind the
formGroup property of the <form> tag to the name of our form which is registerForm.
The
onSubmit method will be invoked on submitting the form. We will also bind the formControlName property of each input field to the control name of our FormGroup. We will check for the errors in the form controls and then display the appropriate validation error message on the screen.Create the nav-bar component
Run the following command to create the nav-bar component.
Open
nav-bar.component.html and put the following code in it.
We are adding the navigation link to the reactive form component in the nav bar.
Update the app component
Open the
app.component.html file and put the following code in it.Update the App module
Add the following code in the
app.module.ts file. We will import the forms module and define the routing for our application. You can refer to GitHub for the complete source code of this file.Execution demo
We will use the following command to start the webserver.
This command will launch the application in your default browser at
http://localhost:4200/. You can perform all the form validations which we have discussed here. This application is also hosted at https://ng-forms-validation.herokuapp.com/. Navigate to the link and play around for a better understanding.Summary
We have created a sample user registration form using the reactive form approach in Angular. We have implemented the inbuilt validations as well as custom validations to the form. The Bootstrap library is used to style the form.
Get the source code from GitHub and play around for a better understanding.


0 comments:
Post a Comment
Note: only a member of this blog may post a comment.