Skip to content

Instantly share code, notes, and snippets.

@alireza-ahmadi
Created August 27, 2014 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alireza-ahmadi/956324f5542caebed5a7 to your computer and use it in GitHub Desktop.
Save alireza-ahmadi/956324f5542caebed5a7 to your computer and use it in GitHub Desktop.
angular.module('foobar', [])
.directive('timeMask', function(){
function format(input){
var time;
time = input.split(':');
if(time.length){
if(time[0] == ''){
time[0] = '00';
time[1] = '00';
}
else if(time[1] == ''){
time[1] = '00';
}
if(parseInt(time[0], 10) > 23){
time[0] = 23;
}
else if(parseInt(time[0], 10) < 10){
time[0] = '0' + parseInt(time[0], 10);
}
if(parseInt(time[1], 10) > 59){
time[1] = 59;
}
else if(parseInt(time[1], 10) < 10){
time[1] = '0' + parseInt(time[1], 10);
}
}
if(time.length == 1){
time[1] = '00';
}
return time.join(':');
}
return {
require : 'ngModel',
restrict : 'A',
link : function(scope, element, attrs, ctrl){
scope.$watch(element, function() {
ctrl.$setViewValue(format(element.val()));
ctrl.$render();
});
element.bind('blur', function() {
ctrl.$setViewValue(format(element.val()));
ctrl.$render();
scope.$apply();
});
}
}
});
<input type="text" ng-model="foo" time-mask />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment