AngularJS Form Validation (Required, Email, Number & Date) Examples

AngularJS Form Validation (Required, Email, Number & Date) Examples

In web technology validation is used to prevent spam entries. There are various kind of form fields validations. Some of them are Required field validation, email validation, date field validation or number validation. In this demo app I created these 4 AngularJS Form Validation (Required Field, Email, Number and Date).

In the below app I have 4 form fields Name, Email, Contact Number & Date of Birth. Name is a required field. Without Name an user is not allowed to submit the form. As an advanced feature AngularJS made validations easy. By simply adding a single line of code ng-show="$error.required"
I can easily made Name field as mandatory.

To valid an email here I used pattern. Validating the pattern against email entry using AngularJS ng-pattern directive. Similar thing I did for Number validation is Contact Number field. To validate Date field in the beginning of script block I declared a variable for date pattern with value YYYY-MM-DD. Using ng-pattern in date field validating the date pattern variable.


<!Doctype html>
<html lang="en">
<title>AngularJS Form Validation Example</title>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
var validateApp=angular.module('validateAngularApp',[]);
/*Page Title goes Here*/
$scope.title="Validations using AngularJS";
/*Date pattern you can update Here*/
$scope.userRegester=function(name, email, contact, dob) {
if(name!=null || email!=null || contact!=null || dob!=null)
alert("Registration Successfull !!!");
alert("Not Successfull Please try again.");
<body ng-app="validateAngularApp" ng-controller="validateAngularctrl">
<div role="main">
<h2>Registration User Form</h2>
<form role="form-inline" name="userForm" action="" method="" novalidate>
<label for="name">Name:</label>
<input type="text" name="name" ng-model="uname" placeholder="Enter your name" required />
<!-- show an error if this isn't filled in -->
<span ng-show="$error.required" ng-style="{color:'red'}">Your name is required.</span>
<label for="email">Email:</label>
<input type="email" name="email" ng-model="uemail" placeholder="Enter your email" ng-pattern="/^[a-z]+[a-z0-9._][email protected][a-z]+\.[a-z.]{2,5}$/ " required/>
<span ng-show="$error.required" ng-style="{color:'red'}">Your email is required .</span>
<span ng-show="$error.pattern" ng-style="{color:'red'}">Your email should be in correct format ex [email protected] .</span>
<label for="contact Number">Contact Number:</label>
<input type="text" name="contactNumber" ng-model="ucontact" placeholder="Enter your Contact details" ng-pattern="/^[0-9]{10,10}$/" ng-minlength="10" ng-maxlength="10" required />
<span ng-show="userForm.contactNumber.$error.required" ng-style="{color:'red'}">Your contact is required .</span>
<span ng-show="userForm.contactNumber.$error.pattern" ng-style="{color:'red'}">Mobile should be numeric</span>
<span ng-show="userForm.contactNumber.$error.minlength" ng-style="{color:'red'}">contact number should be minimum of 10 digits .</span>
<span ng-show="userForm.contactNumber.$error.maxlength" ng-style="{color:'red'}">contact number should be maximum of 10 digits .</span>
<label for="date_of_birth">Date of Birth:</label>
<input type="text" name="dob" ng-model="udob" placeholder="Enter your date of birth in the format of YYYY-MM-DD" ng-pattern="datepattern"required />
<span ng-show="userForm.dob.$error.required" ng-style="{color:'red'}">Your date of birth is required </span>
<span ng-show="userForm.dob.$error.pattern" ng-style="{color:'red'}">Date of Birth must be in the format of YYYY-MM-DD</span>
<button type="submit" ng-click="userRegester(e1,e2,e3,e4)">Register</button>
<footer>&copy; All right reserved</footer>