EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials AngularJS Tutorial AngularJS Validation
Secondary Sidebar
AngularJS Tutorial
  • AngularJS Basic and Advance
    • What is AngularJS
    • Career In AngularJS
    • Uses of Angular JS
    • AngularJS Architecture
    • Angular JS Application
    • AngularJS Application
    • AngularJS Filter Array
    • AngularJS ng-style
    • AngularJs ng init
    • AngularJs ng-repeat
    • AngularJs ng-change
    • AngularJs ng-options
    • AngularJs expressions
    • AngularJs onclick
    • AngularJS ng-show
    • AngularJs ng-include
    • Scope in AngularJS
    • AngularJS ng-if
    • AngularJS Number Filter
    • AngularJS Date Filter
    • AngularJS ng-class
    • AngularJS ng-model
    • AngularJS ng-disabled
    • AngularJS Custom Filter
    • AngularJS Validation
    • AngularJS Filters
    • Controllers in AngularJS
    • AngularJS Animations
    • AngularJS Services
    • AngularJS Events
    • AngularJS Versions
    • AngularJS Directives
    • AngularJS Unit Testing
    • AngularJS with Bootstrap
    • AngularJS UI Bootstrap
    • AngularJS Routing Example
  • Interview Question
    • Angular JS Interview Questions

AngularJS Validation

By Shalu SharmaShalu Sharma

AngularJS Validation

Introduction to AngularJS Validation

AngularJS offers client-side form validation. We use AngularJS as a frontend framework that is very scalable and easy to code and developed our single-page angular web application. Apart from this the AngularJS validation also provides us some more exciting features like form validation which validates the user input to the client-side only without sending them to the backend server for validation. Also, it reduces our backend code and complexity for code maintaining. In angular, we have form validation and we can also create custom validation as well.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,198 ratings)

States of AngularJS Validation

AngularJS provides us form validation to check user input data. This is used as a client-side validation before submitting them to the server. AngularJS keeps track of all the input fields we provide users to enter their data it can be anything like select from the drop-down, input field, text-area, etc. If we have done any kind of modification on the user field it keeps notify the user about the current status of validation, it also keeps track of files that are modified, have been touched or not.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

So in angular js, we have divided the states into two parts. They are specific to form and fields.

1. Input Filed State

As we provide different input fields into the form so they have a different state which is as following and all these fields have the value as Boolean true or false:

  • $dirty: This state shows that the filed is modified or not.
  • $untouched: This state is basically used to show that the current form filed is touched or not.
  • $valid: This file is important because it shows that the input data we have provided is valid according to the input asked to enter.
  • $touched: It simple used to show the field is touched.
  • $invalid: This file is used to show that the input we have provided is invalid according to the input we have asked to enter.
  • $pristine: This uses to show that we have not done anything on the filed yet means has not modified by the user.

2. Form State

The form state which is as follows now all the state will apply for whole form fields, not for specific one or two:

  • $valid: Apply to the whole form used to show fields are valid.
  • $dirty: Used to show more than one field is modified or not.
  • $submitted: This state is used to show that the form is submitted.
  • $pristine: Apply to the whole form to show more than one filed has not modified yet.
  • $invalid: This is used to show that the form fields are invalid corresponding to the input asked to enter.

Similarly, all these form states also have Boolean values true or false. These states we can use to show some messages to the user or we can say error messages or success messages.

Following is an example to Show Validation:

Code:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="">
<p>Now we will leave this first field to be blank to see validation</p>
<form name="myForm">
<p>Name of user:
<input name="myName1" ng-model="myName1" required>
<span ng-show="myForm.myName1.$touched && myForm.myName1.$invalid">User name cannot be blank.</span>
</p>
<p>Address of user:
<input name="myAddress1" ng-model="myAddress1" required>
</p>
</form>
<p>Here we are using ngshow to hide and show the error message if the user has touched the filed.
</p>
</body>
</html>

Output:

AngularJS Validation 1-1

Examples of AngularJS Validation

In angular, we have some in-build validation like email and required which can directly be applied to the fields to validate them.

Example #1

Email Validation.

If we have user input that contains the email of the user, but we need to verify the input data so we can directly make this filed as type email. it is the feature of HTML5. below is one example to show how to use this.

Code:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="">
<p>Please provide your email address into the input field :</p>
<form name="myForm1">
<input type="email" name="myInput" ng-model="myInput">
</form>
<p>State is :</p>
<h1>{{myForm1.myInput.$valid}}</h1>
<p>By default email state is true but once we start writing it will start validating the data whether it is valid or not.</p>
</body>
</html>

Output:

Email Validation

When we provide correct email format it will show below output:

AngularJS Validation 1-3

Example #2

Required Validation.

This is also an HTML5 feature that makes the filed as required. below is a simple example to show how to use this.

Code:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="">
<p>Please provide user name into the field:</p>
<form name="myForm1">
<input name="myInput1" ng-model="myInput1" required>
</form>
<p>State is :</p>
<h1>{{myForm1.myInput1.$valid}}</h1>
</body>
</html>

Output:

Required

When the input field will remain empty it will show below output:

AngularJS Validation 1-5

We can also have custom validation, which means we can build our own validation according to the requirement. But that is not easy we need to make so many things that make the code tricky to understand. For this we need to add a new directive to our application as follows.

Example #3

Custom Validation.

Code:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp">
<p>Please provide details ::</p>
<form name="myForm">
<input name="myInput" ng-model="myInput" required my-directive1>
</form>
<p>State is:</p>
<h1>{{myForm.myInput.$valid}}</h1>
<script>
var app = angular.module('myApp', []);
app.directive('myDirective1', function() {
return {
require: 'ngModel',
link: function(scope, element, attr, mCtrl) {
function myValidation1(value) {
if (value.indexOf("a") > -1) {
mCtrl.$setValidity('charE', true);
} else {
mCtrl.$setValidity('charE', false);
}
return value;
}
mCtrl.$parsers.push(myValidation1);
}
};
});
</script>
<p>Our input must contain the character 'a' to make this input filed valid.</p>
</body>
</html>

Output:

input filed

Here we adding a new directive named mydirective1. It has some naming convection should be camel case, but when we try to call the directive it should be separated by ‘-‘. We are also returning an object to ngModel. Now we will make one function that takes argument according to requirement and here mCTRL1 is the ngModelCotroller. Now test the input by providing some value wrong and right it will return true or false based on the value passed.

Conclusion

So AngularJS validation provides us client-side validation before submitting it to the back end. So it reduces some backend calls to improve performance. Also, we can create custom validations as well by creating a directive in AngularJS and link them with the controller and provide logic in functions.

Recommended Articles

This is a guide to AngularJS Validation. Here we discuss the introduction and states of AngularJS validation along with different examples and code implementation. You may also look at the following articles to learn more –

  1. AngularJS Filters
  2. AngularJS Custom Filter
  3. AngularJS Events
  4. AngularJS Currency Filter
Popular Course in this category
Angular JS Training Program (9 Courses, 7 Projects)
  9 Online Courses |  7 Hands-on Projects |  64+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more