Introduction

In this post, we are going to see how to create a custom validator directive in Angular 5. We have already seen how to do validation in our previous posts, and we have not done any validations for comparing the password and confirm the password, remember? Here we are going to look into that. At the end of this article, you will get to know how to create a new shared directive according to the requirements of your app. This post is a continuation of the Developing an Angular 5 App series. If you haven't gone through the previous posts yet, I strongly recommend you to do that. You can find the links to the previous posts below. I hope you will like this article.

Developing an Angular 5 App Series

These are the previous posts in this series. Please go ahead and have a look.

Background

Validations have a vital role in all application no matter in what language it is being developed. And since it is an essential part, there are many ways to achieve it. If we create a custom shared directive for creating a compare validator, it would be a great piece of code, which can be reused.

Using the Code

It is recommended to clone the project from GitHub so that you can try everything on your own. Let's go ahead and write some code now.

Creating a Custom Directive

Let's just create a new directive in a shared folder and name it equal.validator.directive.ts. Now open that file and start writing the code. Let's import some of the core components first.

As you can see, we are actually inheriting our new class EqualValidatorDirectivefrom Validator. Now we need to change the implementations of the method inside it. We should also add our new directive tp tje app.module.ts file.

Before we do that, we should add our new custom directive selector in our confirm password field, because that's where we are going to use our validator. So we are going to change our markup for the confirm password field as shown below.

As you can see, we are using the selector appEqualValidator="password"in our confirm password field. Please do check my previous posts if you are not sure how to implement other validations like email and required.

Here, we get our confirmPasswordcontrol in the absolute control "c", and in the next line, we are just finding our password control by getting the parent element of confirmPassword. Once we get that, we can easily perform the comparison easily right? So, if it matches we return {"equal": true}; or else {"notEqual": true}.

Introduce New Span for Custom Message

Now we have a custom validator which compares two values, and the only thing which pending is to create a span for showing our new message in the UI. Let's change our markup now.

As you can see, we are showing the custom message only if the directive returns the notEqualproperty and there are no required errors. Let's run our application and see it in action.

Here we have seen how we can implement shared custom validator directive.

Thanks a lot for reading. Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.