Validations using AngularJS in MVC

We will see the validations like Required Field Validation, Email Validation, Data type validation and how to enable the button after getting valid inputs.

We are going to use states for input fields $pristine and $invalid.

ng-class, ng-pattern, ng-show, ng-model also we will use in this article.

Here are the steps,

Step 1 - Add NuGet Package for AngularJS in your solution.

Right click on project and select option Manage NuGet Packages… it will give you a popup for selecting the package. Type AngularJS in the search box it will give you many options but just select the AngularJS only as shown in the image. I have added this already that’s why it's showing tick mark to that option. You will get Install button, just clic and it will add the all files under the Scripts folder.

explorer

explorer

explorer

Step 2 - Drag & drop Angular.min.js

Just drag and drop angular.min.js in your HTML page. It will look like this.

  1. <script src="~/Scripts/angular.min.js"></script>  
Step 3 - Write the below code for Controller and $scope,
  1. <script>  
  2.     var app = angular.module('myApp', []);  
  3.     app.controller('validate', function($scope) {});  
  4. </script>  
Step 4 - Here is your final code,
  1. <script src="~/Scripts/angular.min.js"></script>  
  2. <script>  
  3.     var app = angular.module('myApp', []);  
  4.     app.controller('validate', function($scope) {});  
  5. </script>  
  6.   
  7. <form class="form-horizontal" name="frmApplication" ng-submit="submitForm()" novalidate ng-app="myApp">  
  8.     <fieldset>  
  9.         <br />  
  10.         <div class="row col-md-12" ng-controller="validate">  
  11.             <div class="form-horizontal" role="form">  
  12.                 <div class="form-group" ng-class="{ 'has-error' : frmApplication.firstName.$invalid && !frmApplication.firstName.$pristine }">  
  13.                     <label for="FirstName" class="col-sm-4 control-label">First Name :</label>  
  14.                     <div class="col-sm-8">  
  15.                         <input type="text" name="firstName" id="firstName" class="form-control" ng-model="Application.firstName" placeholder="Enter First Name" required />  
  16.                         <p ng-show="frmApplication.firstName.$invalid && !frmApplication.firstName.$pristine" class="help-block">First name is required</p>  
  17.                     </div>  
  18.                 </div>  
  19.                 <div class="form-group" ng-class="{ 'has-error' : frmApplication.lastName.$invalid && !frmApplication.lastName.$pristine }">  
  20.                     <label for="LastName" class="col-sm-4 control-label">Last Name :</label>  
  21.                     <div class="col-sm-8">  
  22.                         <input type="text" name="lastName" id="lastName" class="form-control" ng-model="Application.lastName" placeholder="Enter Last name" required />  
  23.                         <p ng-show="frmApplication.lastName.$invalid && !frmApplication.lastName.$pristine" class="help-block"> Last name is required</p>  
  24.                     </div>  
  25.                 </div>  
  26.                 <div class="form-group" ng-class="{ 'has-error' : frmApplication.contactNumber.$invalid && !frmApplication.contactNumber.$pristine }">  
  27.                     <label for="contactNumber" class="col-sm-4 control-label">Contact Number :</label>  
  28.                     <div class="col-sm-8">  
  29.                         <input type="number" name="contactNumber" id="contactNumber" class="form-control" ng-model="Application.contactNumber" placeholder="Enter Contact Number" required />  
  30.                         <p ng-show="frmApplication.contactNumber.$invalid && !frmApplication.contactNumber.$pristine" class="help-block">Enter number only</p>  
  31.                     </div>  
  32.                 </div>  
  33.                 <div class="form-group" ng-class="{ 'has-error' : frmApplication.Email.$invalid && !frmApplication.Email.$pristine }">  
  34.                     <label for="Email" class="col-sm-4 control-label">Email Id :</label>  
  35.                     <div class="col-sm-8">  
  36.                         <input type="email" name="Email" id="Email" class="form-control" ng-model="Application.Email" placeholder="Enter Email" required />  
  37.                         <p style="padding-bottom:0; margin-bottom:0;" ng-show="frmApplication.Email.$invalid && !frmApplication.Email.$pristine" class="help-block">Enter a valid email.</p>  
  38.                     </div>  
  39.                 </div>  
  40.                 <div class="form-group" ng-class="{ 'has-error' : frmApplication.userid.$invalid && !frmApplication.userid.$pristine }">  
  41.                     <label for="userid" class="col-sm-4 control-label">User Id:</label>  
  42.                     <div class="col-sm-8">  
  43.                         <input type="text" name="userid" id="userid" class="form-control" ng-model="Application.userid" placeholder="Enter User Id" ng-pattern="/^[a-zA-Z0-9]*$/" required />  
  44.                         <p style="padding-bottom:0; margin-bottom:0;" ng-show="frmApplication.userid.$invalid && !frmApplication.userid.$pristine" class="help-block">Spaces & Special characters not allowed </p>  
  45.                     </div>  
  46.                 </div>  
  47.                 <div class="form-group" ng-class="{ 'has-error' : frmApplication.gender.$invalid && !frmApplication.gender.$pristine }">  
  48.                     <label for="Gender" class="col-sm-4 control-label">Gender :</label>  
  49.                     <div class="col-md-8">  
  50.                         <select name="gender" id="gender" class="form-control" ng-model="Application.gender" required>  
  51. <option value="">- Select -</option>  
  52. <option>Male</option>  
  53. <option>Female</option>  
  54. </select>  
  55.                         <p ng-show="frmApplication.gender.$invalid && !frmApplication.gender.$pristine" class="help-block">Select gender</p>  
  56.                     </div>  
  57.                 </div>  
  58.                 <div class="form-group">  
  59.                     <div class="col-md-offset-4 col-md-8">  
  60.                         <button type="submit" ng-disabled="frmApplication.$invalid || frmApplication.$pristine" class="btn btn-primary">  
  61. Submit  
  62. </button>  
  63.                     </div>  
  64.                 </div>  
  65.             </div>  
  66.         </div>  
  67.     </fieldset>  
  68. </form>  
Step 5 - Run your application and you will get the screen like below,

When I type any character first then this error message will appear.

error

It will validate special characters and space. This code is used for it.

ng-pattern="/^[a-zA-Z0-9]*$/"

screens

After running application below screens will appear.

output
output
output
Submit button will get enabled after all input fields get valid values. So by using the above code you will perform validations using Angular in MVC.