AngularJS Validation In MVC - Part Six

Before starting the sixth part I will recommend you to please go through the below parts,

This part will teach you how to,

  1. How to call custom validation in angular JS

Custom validation usually required when you want to call external functionality from your code and validate the control. For example you might need to do database validation for verifying the user name. For this you need following things to be done.

Initial requirement is reference

<script src="~/Scripts/angular.js"></script>

  1. Add directive in your angular js function. This function will verify is your control value is from specific string names. You may modify by giving service call.
    1. app.directive('myDirective', function()   
    2. {  
    3.     return   
    4.     {  
    5.         require: 'ngModel',  
    6.         link: function(scope, element, attr, mCtrl)  
    7.         {  
    8.             function myValidation(value)  
    9.             {  
    10.                 var myarr = ["India""ind""india""Ind"];  
    11.                 if (myarr.indexOf(value) > -1)   
    12.                 {  
    13.                     mCtrl.$setValidity('charE'true);  
    14.                 } else   
    15.                 {  
    16.                     mCtrl.$setValidity('charE'false);  
    17.                 }  
    18.                 return value;  
    19.             }  
    20.             mCtrl.$parsers.push(myValidation);  
    21.         }  
    22.     };  
    23. });  
  2. Refer your directive in the control to be validate as below way,
    1. <input name="Country" ng-model="Country" required my-directive>   
  3. Give Proper message where you want to give validation message,
    1. <li ng-show="myForm.Country.$invalid">Country should be India/Ind.</li>  
  4. Run the program and you will find then validation working as below


    Hope you will enjoy displaying validation summary box.

Up Next
    Ebook Download
    View all
    View all