Thank you for reading my previous articles. If you have not read them, then study from the following links. After reading them it will be easier to understand today’s topics.

Validation

AngularJS allows us to make own custom validation directive where we can check email id, compare password, and bind the dropdown list.

It has some properties which have shown us errors after the form validation.

It is given below:

Let’s start creating projects:

AngularJS

  1. <!DOCTYPE html>
  2. <html ng-app="app">
  3. <head>
  4. <title>Registration</title>
  5. <meta charset="utf-8" />
  6. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
  7. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
  8. </script>
  9. <script>
  10. var app = angular.module("app", []);
  11. app.directive("ngCompare", function ()
  12. {
  13. return {
  14. require: 'ngModel',
  15. link: function (scope, currentEl, attrs, ctrl)
  16. {
  17. var compareField = document.getElementsByName(attrs.ngCompare)[0];
  18. var compareEl = angular.element(compareField);
  19. currentEl.on("keyup", function ()
  20. {
  21. var isMatch = compareEl.val() === currentEl.val();
  22. ctrl.$setValidity('compare', isMatch);
  23. scope.$digest();
  24. });
  25. compareEl.on("keyup", function ()
  26. {
  27. var isMatch = compareEl.val() === currentEl.val();
  28. ctrl.$setValidity('compare', isMatch);
  29. scope.$digest();
  30. });
  31. }
  32. }
  33. });
  34. app.controller("ctrl", function ($scope)
  35. {
  36. $scope.IsSubmit = false;
  37. $scope.CountryList = [
  38. {
  39. id: 1,
  40. name: "India"
  41. },
  42. {
  43. id: 2,
  44. name: "America"
  45. }];
  46. $scope.CityList = [];
  47. $scope.$watch("user.Country", function (newVal, oldVal)
  48. {
  49. console.log(newVal);
  50. if (newVal == 1)
  51. {
  52. $scope.CityList = [
  53. {
  54. id: 1,
  55. name: "Noida"
  56. },
  57. {
  58. id: 2,
  59. name: "Delhi"
  60. },
  61. {
  62. id: 3,
  63. name: "Allahabad"
  64. },
  65. {
  66. id: 4,
  67. name: "Varanasi"
  68. }];
  69. }
  70. elseif(newVal == 2)
  71. {
  72. $scope.CityList = [
  73. {
  74. id: 5,
  75. name: "Kaliforniya"
  76. },
  77. {
  78. id: 6,
  79. name: "newjurcy"
  80. },
  81. {
  82. id: 7,
  83. name: "washington"
  84. },
  85. {
  86. id: 8,
  87. name: "newyork"
  88. }];
  89. }
  90. });
  91. $scope.SaveData = function ()
  92. {
  93. $scope.IsSubmit = true;
  94. if ($scope.UserForm.$valid)
  95. {
  96. alert("Form is Valid!");
  97. }
  98. else
  99. {
  100. alert("Form is Invalid!");
  101. }
  102. }
  103. });
  104. </script>
  105. </head>
  106. <body class="container" ng-controller="ctrl">
  107. <h2>User SignUp</h2>
  108. <form name="UserForm" ng-submit="SaveData(user)" class="form-horizontal" novalidate>
  109. <div class="form-group">
  110. <label class="col-md-4">Username:
  111. </label>
  112. <div class="col-md-8">
  113. <input type="text" name="Username" ng-model="user.Username" ng-required="true" class="form-control" ng-minlength="3" ng-maxlength="10" />
  114. <p class="text-danger" ng-show="UserForm.Username.$error.required&& (IsSubmit || UserForm.Username.$dirty)">Please Enter Username:
  115. </p>
  116. <p class="text-danger" ng-show="UserForm.Username.$error.minlength&& (IsSubmit || UserForm.Username.$dirty)">*Username must has atleast three characters
  117. </p>
  118. <p class="text-danger" ng-show="UserForm.Username.$error.maxlength&& (IsSubmit || UserForm.Username.$dirty)">*Username cannot has more than 10 characters
  119. </p>
  120. </div>
  121. </div>
  122. <div class="form-group">
  123. <label class="col-md-4">Full Name:
  124. </label>
  125. <div class="col-md-8">
  126. <input type="text" name="Fullname" ng-model="user.Fullname" ng-required="true" class="form-control" />
  127. <p class="text-danger" ng-show="UserForm.Fullname.$error.required&& (IsSubmit || UserForm.Fullname.$dirty)">*Please Enter Fullname
  128. </p>
  129. </div>
  130. </div>
  131. <div class="form-group">
  132. <label class="col-md-4">Password:
  133. </label>
  134. <div class="col-md-8">
  135. <input type="password" name="Password" ng-model="user.Password" ng-required="true" class="form-control" />
  136. <p class="text-danger" ng-show="UserForm.Password.$error.required&& (IsSubmit || UserForm.Password.$dirty)">*Please Enter Password
  137. </p>
  138. </div>
  139. </div>
  140. <div class="form-group">
  141. <label class="col-md-4">Confirm Password:
  142. </label>
  143. <div class="col-md-8">
  144. <input type="password" name="ConfirmPassword" ng-model="user.ConfirmPassword" ng-required="true" class="form-control" ng-compare="Password" />
  145. <p class="text-danger" ng-show="UserForm.ConfirmPassword.$error.required&& (IsSubmit || UserForm.ConfirmPassword.$dirty)">*Please Enter ConfirmPassword
  146. </p>
  147. <p class="text-danger" ng-show="UserForm.ConfirmPassword.$error.compare&& (IsSubmit || UserForm.ConfirmPassword.$dirty)">**Confirm Password Doesn't match
  148. </p>
  149. </div>
  150. </div>
  151. <div class="form-group">
  152. <label class="col-md-4">Email
  153. </label>
  154. <div class="col-md-8">
  155. <input type="email" name="Email" ng-model="user.Email" ng-required="true" class="form-control" />
  156. <p class="text-danger" ng-show="UserForm.Email.$error.required&& (IsSubmit || UserForm.Email.$dirty)">*Please Enter Email
  157. </p>
  158. <p class="text-danger" ng-show="UserForm.Email.$error.email&& (IsSubmit || UserForm.Email.$dirty)">**Please Enter Correct Email
  159. </p>
  160. </div>
  161. </div>
  162. <div class="form-group">
  163. <label class="col-md-4">Mobile No:
  164. </label>
  165. <div class="col-md-8">
  166. <input type="number" name="Mobile" ng-model="user.Mobile" ng-required="true" class="form-control" />
  167. <p class="text-danger" ng-show="UserForm.Mobile.$error.required&& (IsSubmit || UserForm.Mobile.$dirty)">*Please Enter Mobile no.
  168. </p>
  169. <p class="text-danger" ng-show="UserForm.Mobile.$error.number&& (IsSubmit || UserForm.Mobile.$dirty)">**Please Enter Numbers Only
  170. </p>
  171. </div>
  172. </div>
  173. <div class="form-group">
  174. <label class="col-md-4">Country:
  175. </label>
  176. <div class="col-md-8">
  177. <select name="Country" ng-model="user.Country" ng-options="country.id as country.name for country in CountryList" ng-required="true" class="form-control">
  178. <option value="">Select
  179. </option>
  180. </select>
  181. <p class="text-danger" ng-show="UserForm.Country.$error.required&& (IsSubmit || UserForm.Country.$dirty)">*Please Select Country
  182. </p>
  183. </div>
  184. </div>
  185. <div class="form-group">
  186. <label class="col-md-4">City:
  187. </label>
  188. <div class="col-md-8">
  189. <select name="City" ng-model="user.City" ng-options="city.name for city in CityList" ng-required="true" class="form-control">
  190. <option value="">Select
  191. </option>
  192. </select>
  193. <p class="text-danger" ng-show="UserForm.City.$error.required&& (IsSubmit || UserForm.City.$dirty)">*Please Select Country
  194. </p>
  195. </div>
  196. </div>
  197. <div class="form-group">
  198. <label class="col-md-4">
  199. </label>
  200. <div class="col-md-8">
  201. <input type="submit" value="SignUp" class="btnbtn-primary" ng-disabled="UserForm.$invalid" />
  202. </div>
  203. </div>
  204. </form>
  205. </body>#ff0000</html>
Output

Output

So, today we read about validation on form.

Read more articles on AngularJS: