Chiran Chiran - 2 months ago 40
AngularJS Question

Angular JS directive for automatically insert slashes '/' in date fields. for Example MM/DD/yyyy

I'm trying to add functionality to input date fields so as when the users enters in digits, slashes "/" get automatically added its in Jquery but I need in Angular JS directive.
Thanks in advance

Answer

var app = angular.module('myApp', []);
		  app.controller('appCtrl', ['$scope', function($scope){
		     $scope.change = function(e) {
		     	var numChars = $scope.date.length;
				if(numChars === 2 || numChars === 5){
					var thisVal = $scope.date;
					thisVal += '/';
					$scope.date = thisVal;
				}
		      };
		  }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="appCtrl">
        <h4>Minimum date</h4>
        <input minlength="0"  maxlength="10" placeholder="DD/MM/YYYY" id="date" ng-change="change()" ng-model="date">
     </div> 
Working example. Please try it

Comments