【发布时间】:2015-01-27 09:58:22
【问题描述】:
我有一个角度应用程序,其中输入字段应该只允许带一位小数的正数。在我的指令中,我将替换除 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;
});
}
这个问题可能看起来很无知,但我已经尝试了在此之前提供的所有解决方案,但根据相同的方式更改我的正则表达式似乎不起作用。它现在接受多个'.'。
提前致谢。
【问题讨论】:
标签: regex angularjs angularjs-directive