AngularJS Validation In MVC - Part Three

Before starting the third part I recommend that you please go through the previous parts on AngularJS Validation:

This part will teach you how to,
  1. Apply CSS to validation message
  2. Disable or enable the button depending on validation message
  3. Change the color of controls as per validation

Initial requirement is reference

  1. <script src="~/Scripts/angular.js"></script>   
  1. Apply CSS to validation message

    i. Below CSS will help you to apply css to the validation message,
    1. .ValidationMessage {  
    2. color: red;  
    3. font-size: x-small;  
    4. }  
    And to the control of message apply below way,
    1. <span class="ValidationMessage" ng-show="myForm.email.$dirty && myForm.email.$invalid">  
    2.                 <br />  
    3.                 <span ng-show="myForm.email.$error.required">Email is required.</span>  
    4.                 <span ng-show="myForm.email.$error.email">Invalid email address.</span>  
    5.             </span>  

  2. Disable or enable the save button depending on validation

    By below way you can enable / disable the save button control on validation,
    1. <button ng-disabled="myForm.$invalid">Save</button>  
  3. Change the controls as per validation.

    For Invalid control CSS is,
    1. input.ng-invalid {  
    2. background-color: pink;  
    3. }  
    For valid control CSS is,
    1. input.ng-valid {  
    2. background-color: lightgreen;  
    3. }  

My final code will look like 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.   
  7.         <p>  
  8.             Student Roll Number:<br>  
  9.             <input type="number" name="StudentRollNumber" ng-model="StudentRollNumber" required>  
  10.   
  11.             <span class="ValidationMessage" ng-show="myForm.StudentRollNumber.$dirty && myForm.StudentRollNumber.$invalid">  
  12.                 <br />  
  13.                 <span ng-show="myForm.StudentRollNumber.$error.required">Student Roll Number is required.</span>  
  14.                 <span ng-show="myForm.StudentRollNumber.$error.number">Not valid number!</span>  
  15.             </span>  
  16.         </p>  
  17.   
  18.   
  19.         <p>  
  20.             Student Name:<br>  
  21.             <input type="text" name="Student" ng-model="Student" required>  
  22.             <span class="ValidationMessage" ng-show="myForm.Student.$dirty && myForm.Student.$invalid">  
  23.                 <br />  
  24.                 <span ng-show="myForm.Student.$error.required">Student Name is required.</span>  
  25.             </span>  
  26.         </p>  
  27.   
  28.         <p>  
  29.             Student Birth Date:<br>  
  30.             <input type="date" name="BirthDate" ng-model="BirthDate" required placeholder="yyyy-MM-dd">  
  31.             <span class="ValidationMessage" ng-show="myForm.BirthDate.$dirty && myForm.BirthDate.$invalid">  
  32.                 <br />  
  33.                 <span ng-show="myForm.BirthDate.$error.required">Student Birth Date is required.</span>  
  34.                 <span ng-show="myForm.BirthDate.$error.date">Not a Valid Date.</span>  
  35.             </span>  
  36.         </p>  
  37.   
  38.   
  39.   
  40.         <p>  
  41.             Student Email:<br>  
  42.             <input type="email" name="email" ng-model="email" required>  
  43.             <span class="ValidationMessage" ng-show="myForm.email.$dirty && myForm.email.$invalid">  
  44.                 <br />  
  45.                 <span ng-show="myForm.email.$error.required">Email is required.</span>  
  46.                 <span ng-show="myForm.email.$error.email">Invalid email address.</span>  
  47.             </span>  
  48.         </p>  
  49.   
  50.   
  51.   
  52.         <p>  
  53.             Student Percentage Marks:<br>  
  54.             <input type="number" name="marks" ng-model="marks" max="100" required>  
  55.             <span class="ValidationMessage" ng-show="myForm.marks.$dirty && myForm.marks.$invalid">  
  56.                 <br />  
  57.                 <span ng-show="myForm.marks.$error.required">Email is required.</span>  
  58.                 <span ng-show="myForm.marks.$error.number">Invalid number.</span>  
  59.                 <span ng-show="myForm.marks.$error.max">Max Percentage is 100.</span>  
  60.             </span>  
  61.         </p>  
  62.   
  63.   
  64.         <br><br>  
  65.         <button ng-disabled="myForm.$invalid">Save</button>  
  66.   
  67.         <p>  
  68.     </fieldset>  
  69. </form>  
  70.   
  71. <script>  
  72.         var app = angular.module('myApp', []);  
  73.         app.controller('validateCtrl', function ($scope) {  
  74.             StudentRollNumber = 12;  
  75.             BirthDate = new Date(2013, 9, 22);  
  76.         });  
  77. </script>  
  78.   
  79. <style>  
  80.     input.ng-invalid {  
  81.         background-color: pink;  
  82.     }  
  83.   
  84.     input.ng-valid {  
  85.         background-color: lightgreen;  
  86.     }  
  87.   
  88.     form.ng-pristine {  
  89.         background-color: lightblue;  
  90.     }  
  91.   
  92.     .ValidationMessage {  
  93.         color: red;  
  94.         font-size: x-small;  
  95.     }  
  96. </style>  
Run your application you will find the error message working as below, 
  1. Invalid State : Check the CSS of message,
    Invalid State
  2. Valid state control CSS will looks as below,

    valid
    And in valid state Save button will get enabled.

So by this way you can apply the CSS , you can change the color of the control in invalid state as well as valid state and Enable or disable the control in valid state.

Read more articles on AngularJS:
 
.Net Serialization using Soap Formatter

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now