-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
106 lines (87 loc) · 4.24 KB
/
script.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Code goes here
var app = angular.module('testapp',[]);
app.controller('appController', function ($scope) {
$scope.positiveNumber = 0;
$scope.negativeNumber = 0;
$scope.decimal = 0;
$scope.decimalUpto = 0
$scope.negativedecimal = 0;
$scope.negativedecimalUpto = 0
$scope.total = 0;});
angular.module('testapp').directive('validNumber', function () {
return {
require: '?ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
element.on('keydown', function (event) {
var keyCode=[]
if(attrs.allowNegative == "true")
{ keyCode = [8, 9, 36, 35, 37, 39, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 109, 110, 173, 190,189];
}
else{
var keyCode = [8, 9, 36, 35, 37, 39, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 110, 173, 190];
}
if(attrs.allowDecimal == "false") {
var index = keyCode.indexOf(190);
if (index > -1) {
keyCode.splice(index, 1);
}
}
if ($.inArray(event.which, keyCode) == -1) event.preventDefault();
else {console.log(2);
var oVal = ngModelCtrl.$modelValue || '';
if ($.inArray(event.which, [109, 173]) > -1 && oVal.indexOf('-') > -1) event.preventDefault();
else if ($.inArray(event.which, [110, 190]) > -1 && oVal.indexOf('.') > -1) event.preventDefault();
}
})
.on('blur', function () {
if (element.val() == '' || parseFloat(element.val()) == 0.0 || element.val() == '-') {
ngModelCtrl.$setViewValue('0.00');
}
else if(attrs.allowDecimal == "false")
{
ngModelCtrl.$setViewValue(element.val());
}
else{
if(attrs.decimalUpto)
{
var fixedValue = parseFloat(element.val()).toFixed(attrs.decimalUpto);}
else{ var fixedValue = parseFloat(element.val()).toFixed(2);}
ngModelCtrl.$setViewValue(fixedValue);
}
ngModelCtrl.$render();
scope.$apply();
});
ngModelCtrl.$parsers.push(function (text) {
var oVal = ngModelCtrl.$modelValue;
var nVal = ngModelCtrl.$viewValue;
console.log(nVal);
if (parseFloat(nVal) != nVal) {
if (nVal === null || nVal === undefined || nVal == '' || nVal == '-') oVal = nVal;
ngModelCtrl.$setViewValue(oVal);
ngModelCtrl.$render();
return oVal;
}
else {
var decimalCheck = nVal.split('.');
if (!angular.isUndefined(decimalCheck[1])) {
if(attrs.decimalUpto)
decimalCheck[1] = decimalCheck[1].slice(0, attrs.decimalUpto);
else
decimalCheck[1] = decimalCheck[1].slice(0, 2);
nVal = decimalCheck[0] + '.' + decimalCheck[1];
}
ngModelCtrl.$setViewValue(nVal);
ngModelCtrl.$render();
return nVal;
}
});
ngModelCtrl.$formatters.push(function (text) {
if (text == '0' || text == null && attrs.allowDecimal == "false") return '0';
else if (text == '0' || text == null && attrs.allowDecimal != "false" && attrs.decimalUpto == undefined) return '0.00';
else if (text == '0' || text == null && attrs.allowDecimal != "false" && attrs.decimalUpto != undefined) return parseFloat(0).toFixed(attrs.decimalUpto);
else if (attrs.allowDecimal != "false" && attrs.decimalUpto != undefined) return parseFloat(text).toFixed(attrs.decimalUpto);
else return parseFloat(text).toFixed(2);
});
}
};
});