【问题标题】:Directive to allow decimal numbers with only 1 decimal point允许只有 1 个小数点的十进制数的指令
【发布时间】: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


    【解决方案1】:

    这是小提琴http://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;
                            }
                        }
                    });
                }
            };
        });
    

    【讨论】:

    • 感谢您的示例。我得到了很多帮助。
    【解决方案2】:

    我明白了。我没有找到正则表达式解决方案,所以它使用了一些 javascript 字符串操作。 PFB 代码:

                var firstIndexOfDecimal = transformedInput.indexOf('.');
                var lastIndexofDecimal = transformedInput.lastIndexOf(".");
    
                if(firstIndexOfDecimal !== lastIndexofDecimal){
                    transformedInput = transformedInput.substr(0,lastIndexofDecimal) + transformedInput.substr(lastIndexofDecimal+1, transformedInput.length);
                }
    

    感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      • 2019-04-22
      • 1970-01-01
      • 2023-03-21
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多