Float directive

I changed the float directive last week and now i'm reverting it back. The change i made didn't account for neagative numbers so i'm reverting back
This commit is contained in:
Jared Tabor
2014-12-01 22:10:34 -05:00
parent a7066443f0
commit 98fa7eae40

View File

@@ -183,19 +183,14 @@ angular.module('AWDirectives', ['RestServices', 'Utilities', 'AuthService', 'Job
}]) }])
.directive('smartFloat', function() { .directive('smartFloat', function() {
var FLOAT_REGEXP_1 = /^\$?\d+(.\d{3})*(\,\d*)?$/, //Numbers like: 1.123,56 var FLOAT_REGEXP = /^\-?\d+((\.|\,)\d+)?$/;
FLOAT_REGEXP_2 = /^\$?\d+(,\d{3})*(\.\d*)?$/; //Numbers like: 1,123.56
return { return {
restrict: 'A',
require: 'ngModel', require: 'ngModel',
link: function (scope, elm, attrs, ctrl) { link: function(scope, elm, attrs, ctrl) {
ctrl.$parsers.unshift(function (viewValue) { ctrl.$parsers.unshift(function(viewValue) {
if (FLOAT_REGEXP_1.test(Number(viewValue))) { if (FLOAT_REGEXP.test(viewValue)) {
ctrl.$setValidity('float', true); ctrl.$setValidity('float', true);
return parseFloat(viewValue.replace('.', '').replace(',', '.')); return parseFloat(viewValue.replace(',', '.'));
} else if (FLOAT_REGEXP_2.test(Number(viewValue))) {
ctrl.$setValidity('float', true);
return parseFloat(viewValue.replace(',', ''));
} else { } else {
ctrl.$setValidity('float', false); ctrl.$setValidity('float', false);
return undefined; return undefined;
@@ -203,21 +198,6 @@ angular.module('AWDirectives', ['RestServices', 'Utilities', 'AuthService', 'Job
}); });
} }
}; };
// var FLOAT_REGEXP = /^\-?\d+((\.|\,)\d+)?$/;
// return {
// require: 'ngModel',
// link: function(scope, elm, attrs, ctrl) {
// ctrl.$parsers.unshift(function(viewValue) {
// if (FLOAT_REGEXP.test(viewValue)) {
// ctrl.$setValidity('float', true);
// return parseFloat(viewValue.replace(',', '.'));
// } else {
// ctrl.$setValidity('float', false);
// return undefined;
// }
// });
// }
// };
}) })
// integer Validate that input is of type integer. Taken from Angular developer // integer Validate that input is of type integer. Taken from Angular developer