Follow by Email

Friday, December 5, 2014

Angularjs : HTML form validation example

hi let see how to do validate a html form using angularjs
example :
<!DOCTYPE html>
<script src=””></script&gt;
function EmpForm($scope) {
$scope.empname = ‘Chandan Singh';
$scope.empemail = ‘';
<form ng-app=”” ng-controller=”EmpForm” name=”EmpFormPage” novalidate>
Emp name:
<input type=”text” name=”empname” ng-model=”empname” required>
<span ng-show=”EmpFormPage.empname.$error.required”>Please Enter Emp Name</span>
<input type=”email” name=”empemail” ng-model=”empemail” required>
<span ng-show=”EmpFormPage.empemail.$error.required”>Please Enter Email Address</span>
<span ng-show=”EmpFormPage.empemail.$”>Invalid Email Address</span>
<input type=”submit” ng-disabled=”EmpFormPage.empname.$invalid || EmpFormPage.empemail.$invalid”>
  • ng-show directive show and hides the error messages as per the expression provided into it.
  • ng-disabled directive will disable the submit button till all the fields are validated properly