Before starting the second part I recommend that you please go through the first part.

AngularJS Validation In MVC - Part One

This part will teach you how to validate the Number, Email address, Min / Max validation of number. This is all the types of validation you are going to learn here .

Initial requirement is reference,
  1. <script src="~/Scripts/angular.js"></script>
1: Data type validation :

2: Required filed validation

3: Date Validation

4: Email Validation

5: Range Validation Max and Min

Your final code is as below,

  1. <script src="~/Scripts/angular.js"></script>
  2. <form ng-app="myApp" ng-controller="validateCtrl"
  3. name="myForm" novalidate style="width: 470px;margin: 0 auto;border:medium;margin-top:10%">
  4. <fieldset>
  5. <legend>Validation Demo</legend>
  6. <p>
  7. Student Roll Number:<br>
  8. <input type="number" name="StudentRollNumber" ng-model="StudentRollNumber" required>
  9. <span style="color:red" ng-show="myForm.StudentRollNumber.$dirty && myForm.StudentRollNumber.$invalid">
  10. <span ng-show="myForm.StudentRollNumber.$error.required">Student Roll Number is required.</span>
  11. <span ng-show="myForm.StudentRollNumber.$error.number">Not valid number!</span>
  12. </span>
  13. </p>
  14. <p>
  15. Student Name:<br>
  16. <input type="text" name="Student" ng-model="Student" required>
  17. <span style="color:red" ng-show="myForm.Student.$dirty && myForm.Student.$invalid">
  18. <span ng-show="myForm.Student.$error.required">Student Name is required.</span>
  19. </span>
  20. </p>
  21. <p>
  22. Student Birth Date:<br>
  23. <input type="date" name="BirthDate" ng-model="BirthDate" required placeholder="yyyy-MM-dd">
  24. <span style="color:red" ng-show="myForm.BirthDate.$dirty && myForm.BirthDate.$invalid">
  25. <span ng-show="myForm.BirthDate.$error.required">Student Birth Date is required.</span>
  26. <span ng-show="myForm.BirthDate.$error.date">Not a Valid Date.</span>
  27. </span>
  28. </p>
  29. <p>
  30. Student Email:<br>
  31. <input type="email" name="email" ng-model="email" required>
  32. <span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
  33. <span ng-show="myForm.email.$error.required">Email is required.</span>
  34. <span ng-show="myForm.email.$error.email">Invalid email address.</span>
  35. </span>
  36. </p>
  37. <p>
  38. Student Percentage Marks:<br>
  39. <input type="number" name="marks" ng-model="marks" max="100" required>
  40. <span style="color:red" ng-show="myForm.marks.$dirty && myForm.marks.$invalid">
  41. <span ng-show="myForm.marks.$error.required">Email is required.</span>
  42. <span ng-show="myForm.marks.$error.number">Invalid number.</span>
  43. <span ng-show="myForm.marks.$error.max">Max Percentage is 100.</span>
  44. </span>
  45. </p>
  46. <p>
  47. </fieldset>
  48. </form>
  49. <script>
  50. var app = angular.module('myApp', []);
  51. app.controller('validateCtrl', function ($scope) {
  52. StudentRollNumber = 12;
  53. BirthDate = new Date(2013, 9, 22);
  54. });
  55. </script>
Run your application you will find the error message working as below,
output
So in this way you will perform the validation using Angular JS in MVC although MVC has its own beautiful validation framework.
Read more articles on AngularJS: