允许只有 1 个小数点的十进制数的指令
Directive to allow decimal numbers with only 1 decimal point
我有一个 angular 应用程序,其中输入字段应该只允许带一位小数的正数。在我的指令中,我将替换 0-9 和“.”以外的任何内容。但目前我的应用程序正在接受多个十进制值。
它应该接受:
0.5
0.56
没有
0.5.5 或 0..5
PFB代码:
link: function (scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function (inputValue) {
// this next if is necessary for when using ng-required on your input.
// In such cases, when a letter is typed first, this parser will be called
// again, and the 2nd time, the value will be undefined
if (inputValue === undefined) {
return '';
}
var transformedInput = inputValue.replace(/[^0-9\.]/g, '');
if (transformedInput !== inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
}
这个问题可能看起来很无知,但是我已经尝试了之前提供的所有解决方案,但是根据相同的规则更改我的正则表达式似乎不起作用。它现在接受多个 '.'.
提前致谢。
这里是fiddlehttp://jsfiddle.net/oora0t93/检查一下:-
app.directive('inputPrice', function () {
return {
restrict: 'EA',
template: '<input name="{{inputName}}" ng-model="inputValue" />',
scope: {
inputValue: '=',
inputName: '='
},
link: function (scope) {
scope.$watch('inputValue', function(newValue,oldValue) {
if(String(newValue).indexOf(',') != -1)
scope.inputValue = String(newValue).replace(',', '.');
else {
var index_dot,
arr = String(newValue).split("");
if (arr.length === 0) return;
if (arr.length === 1 && (arr[0] == '-' || arr[0] === '.')) return;
if (arr.length === 2 && newValue === '-.') return;
if (isNaN(newValue) || ((index_dot = String(newValue).indexOf('.')) != -1 && String(newValue).length - index_dot > 3 )) {
scope.inputValue = oldValue;
}
}
});
}
};
});
我明白了。我没有找到正则表达式解决方案,所以使用一些 javascript 字符串操作找到了它。 PFB代码:
var firstIndexOfDecimal = transformedInput.indexOf('.');
var lastIndexofDecimal = transformedInput.lastIndexOf(".");
if(firstIndexOfDecimal !== lastIndexofDecimal){
transformedInput = transformedInput.substr(0,lastIndexofDecimal) + transformedInput.substr(lastIndexofDecimal+1, transformedInput.length);
}
感谢您的帮助。
我有一个 angular 应用程序,其中输入字段应该只允许带一位小数的正数。在我的指令中,我将替换 0-9 和“.”以外的任何内容。但目前我的应用程序正在接受多个十进制值。 它应该接受:
0.5 0.56
没有 0.5.5 或 0..5
PFB代码:
link: function (scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function (inputValue) {
// this next if is necessary for when using ng-required on your input.
// In such cases, when a letter is typed first, this parser will be called
// again, and the 2nd time, the value will be undefined
if (inputValue === undefined) {
return '';
}
var transformedInput = inputValue.replace(/[^0-9\.]/g, '');
if (transformedInput !== inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
}
这个问题可能看起来很无知,但是我已经尝试了之前提供的所有解决方案,但是根据相同的规则更改我的正则表达式似乎不起作用。它现在接受多个 '.'.
提前致谢。
这里是fiddlehttp://jsfiddle.net/oora0t93/检查一下:-
app.directive('inputPrice', function () {
return {
restrict: 'EA',
template: '<input name="{{inputName}}" ng-model="inputValue" />',
scope: {
inputValue: '=',
inputName: '='
},
link: function (scope) {
scope.$watch('inputValue', function(newValue,oldValue) {
if(String(newValue).indexOf(',') != -1)
scope.inputValue = String(newValue).replace(',', '.');
else {
var index_dot,
arr = String(newValue).split("");
if (arr.length === 0) return;
if (arr.length === 1 && (arr[0] == '-' || arr[0] === '.')) return;
if (arr.length === 2 && newValue === '-.') return;
if (isNaN(newValue) || ((index_dot = String(newValue).indexOf('.')) != -1 && String(newValue).length - index_dot > 3 )) {
scope.inputValue = oldValue;
}
}
});
}
};
});
我明白了。我没有找到正则表达式解决方案,所以使用一些 javascript 字符串操作找到了它。 PFB代码:
var firstIndexOfDecimal = transformedInput.indexOf('.');
var lastIndexofDecimal = transformedInput.lastIndexOf(".");
if(firstIndexOfDecimal !== lastIndexofDecimal){
transformedInput = transformedInput.substr(0,lastIndexofDecimal) + transformedInput.substr(lastIndexofDecimal+1, transformedInput.length);
}
感谢您的帮助。