AngularJS (45 Blogs) Become a Certified Professional

How to Implement Form Validation in Angular JS?

Last updated on Nov 22,2022 1.3K Views

Validation is a method to authenticate the user. It is used in all the Web Technologies like HTML and JavaScript. But today our focus will be on Validation in Angular JS in the following order:


What is Form validation?

Form validation is a technique by which we can validate the HTML form. Let’s take a simple example to suppose a user has an HTML form and that HTML form has different fields, these field is validated by form validator when we want to validate the form we just need to check the value of the particular field with the validator expression. For more check out this full stack development course now.

validation-in-angular-jsIf regular expression and the field value are the same then we can say our form is validated. In the HTML form, there are different types of validations like email, required, min, max, password, etc.


Form Validation in Angular JS

Let’s talk about how we can validate a form in angular JS. Angular JS provides various type of form validation properties that we can use to validate the form or get the data from the form.

  • $valid: This property tells whether the field is valid or not by applying the appropriate rule on that.

  • $invalid: As the name says invalid this tiles weather the field is invalid or not based on appropriate rule on that.

  • $pristine: It will return true in the form input field is not used.

  • $dirty: It will return true in the form input field is used.

  • $touched: BooleanTrue if the input has been blurred.


To access the form: <form name>.<angular property>

To access an input: <form name>.<input name>.<angular property>

Now let’s explain the form validation in angular JS with an example so firstly we make two files, one is app.js and another is index.html. Our index.htm file contains a simple HTML form that has the angular validation and our app.js file contains the backend code to handle the form validation on the index.html page.

The index.html page contents form with novalidate property and what that means exactly?

The novalidate property in the form tag tells the HTML that we can use our custom form validation. If we don’t give the novalidate property then and the HTML form validates by using HTML5 default form validation properties.


Steps in Form Validation

In our form, we created 6 fields in our form these are first name, last name, email, phone, password, and message. 

  1. First, we add the required field validator, this validator tells the users that a specific field is required.

  2. Next is the email field if a user doesn’t give any valid email then our email validator throws an email validation error.

  3. We set the minimum and the maximum length in our password validation the minimum length is 5 and the maximum length is 8 so the user can give a valid password between 5 to 8 characters.

  4. Finally, we set the phone and the message fields required and specifically, apply number validation on phone filed.



Code for Form Validation in Angular JS


<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Angular scope example</title>
<body ng-app="ngValidApp">

<div ng-controller="ngValidController">

<form name="chkValidation" novalidate>
<label>First Name</label>
<input type="text" placeholder="Enter first name" name="firstName" ng-model="firstName" ng-required="true"><br>
<p style = "color:red" ng-show = "chkValidation.firstName.$error.required">This filed is required</p>

<label>Last Name</label>
<input type="text" placeholder="Enter last name" name="lastName" ng-model="lastName" ng-required="true"><br>
<p style = "color:red" ng-show = "chkValidation.lastName.$error.required">This filed is required</p>

<input type="email" placeholder="Enter email" name="email" ng-model="email" ng-required="true"><br>
<p style = "color:red" ng-show = "$error.required">This filed is required</p>
<p style = "color:red" ng-show = "$">Not a valid email</p>

<input type="text" required placeholder="Enter phone" name="phone" ng-model="phone" ng-required="true" ng-pattern="/^(+d{1,2}s)?(?d{3})?[s.-]?d{3}[s.-]?d{4}$/"><br>
<p style = "color:red" ng-show = "$error.required">This filed is required</p>
<p style = "color:red" ng-show="$error.pattern">This is not a valid phone</p>

<input placeholder="******" type="password" name="password" ng-model="password" ng-required="true" ng-minlength="5" ng-maxlength="8"><br>
<p style = "color:red" ng-show = "chkValidation.password.$error.required">This filed is required</p>
<p style = "color:red" ng-show = "(!chkValidation.password.$valid)">Password between 5 to 8 characters</p>

<textarea required placeholder="Enter your message" name="message" ng-model="message" ng-required="true"></textarea><br>
<p style = "color:red" ng-show = "chkValidation.message.$error.required">This filed is required</p>

<button type="submit">Submit</button>


<!--angular js library-->
<script src="assets/js/angular.js"></script>
<script src="app.js"></script>



var app = angular.module('ngValidApp', []);

app.controller('ngValidController', function($scope) {



Let’s talk about some validation directive used in the form:

  • ng-required: For providing the required field
  • ng-show: To show the error message based on the condition(check the validation properties)
  • ng-minlength: For providing minimum length
  • ng-maxlength: For providing the maximum length
  • ng-pattern: To match a specific pattern
  • ng-model: Binds the field with validation properties like $error, $valid, etc.


With this, we come to an end of this Validation in Angular JS article. I hope you got an understanding of the various things to be considered for form Validation in Angular JS.

If you wish to learn more about Angular framework, then check out our Angular Course which comes with instructor-led live training and real-life project experience. This training will help you understand Angular in-depth and help you achieve mastery over the subject.

Got a question for us? Please mention it in the comments section of this article and We will get back to you.

Upcoming Batches For Angular Certification Training Course
Course NameDate
Angular Certification Training Course

Class Starts on 3rd December,2022

3rd December

SAT&SUN (Weekend Batch)
View Details

Join the discussion

Browse Categories

webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How to Implement Form Validation in Angular JS?