Creating Your Own Directive For Accepting Input In The Input Textbox As A Number Field Only

In this blog, we will see

What are Directives in AngularJS?

  • Actually, they're markers on DOM Elements.
  • With the help of Directives, we can extend HTML with the new attributes.

There are already lots of predefined Directives but apart from that, we can also create our own Directive.

Let us see how to create our own Directive.

Example. Directive for taking the numbers only as an input.

We will learn how to prevent the user from typing anything in the HTML textbox apart from numbers only.

In order to do this,

We need to perform the steps given below.

Step 1

First of all, add Directive given below in your controller.js. Directive is with the name validNumber.

The code snippet is given below for doing it. 

  1. //Adding numeric values.  
  2. angular.module("Sandip-MyModule").directive('validNumber'function ()  
  3. {  
  4.     return {  
  5.         require: '?ngModel',  
  6.         link: function (scope, element, attrs, ngModelControl)  
  7.         {  
  8.             if (!ngModelControl)  
  9.             {  
  10.                 return;  
  11.             }  
  12.   
  13.             ngModelControl.$parsers.push(function (val)  
  14.             {  
  15.                 if (angular.isUndefined(val))  
  16.                 {  
  17.                     var val = '';  
  18.                 }  
  19.   
  20.                 var clean = val.replace(/[^/0-9]/g, '');  
  21.   
  22.                 var nCheck = clean.split('-');  
  23.                 var dCheck = clean.split('.');  
  24.                 if (!angular.isUndefined(nCheck[1]))  
  25.                 {  
  26.                     nCheck[1] = nCheck[1].slice(0, nCheck[1].length);  
  27.                     clean = nCheck[0] + '-' + nCheck[1];  
  28.                     if (nCheck[0].length > 0)  
  29.                     {  
  30.                         clean = nCheck[0];  
  31.                     }  
  32.   
  33.                 }  
  34.   
  35.                 if (!angular.isUndefined(dCheck[1]))  
  36.                 {  
  37.                     dCheck[1] = dCheck[1].slice(0, 2);  
  38.                     clean = dCheck[0] + '.' + dCheck[1];  
  39.                 }  
  40.   
  41.                 if (val !== clean)  
  42.                 {  
  43.                     ngModelControl.$setViewValue(clean);  
  44.                     ngModelControl.$render();  
  45.                 }  
  46.                 return clean;  
  47.             });  
  48.   
  49.             element.bind('keypress'function (event)  
  50.             {  
  51.                 if (event.keyCode === 32)  
  52.                 {  
  53.                     event.preventDefault();  
  54.                 }  
  55.             });  
  56.         }  
  57.     };  
  58. });  

Step 2

Now, let us see how to apply this Directive to your HTML text box.

The code snippet is given below for it 

  1. <div class="row no-margin">  
  2.                     <div class="form-group">  
  3.                         <label class="custom-width-toolsdate">Number of Records: </label>  
  4.                         <input type="text" ng-model="labelsToSkip" ng- maxlength="2" valid-number />  
  5.                 
  6.   </div>   

In the code snippet given above, just observe how it is applied to HTML tag input with type=”text”.

Just use it with the valid number.

Step 3

Now, whenever the user types any characters apart from the number values into the input text box, it won’t allow them to type anything apart from the numbers.

Summary

This is our own directive for taking an input as a number-only value.

Ebook Download
View all
Learn
View all