forked from shanmohan/custom-directives
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirective.js
25 lines (21 loc) · 941 Bytes
/
directive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
angular.module('mohan-directives',[]);
angular.module('mohan-directives').directive('mohanNumericOnly', function(){
return {
require: 'ngModel',
link: function (scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function (inputValue) {
if (inputValue == undefined) return '';
var transformedInput = inputValue.toString().replace(/[^0-9]/g, '');
if (transformedInput != inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
if (transformedInput === "0") {
modelCtrl.$setViewValue('');
modelCtrl.$render();
}
return (transformedInput === "0") ? '' : transformedInput;
});
}
}
});