angularJS ng-pattern无效

时间:2014-03-24 14:37:28

标签: angularjs validation ng-pattern

请看一下那个。

http://plnkr.co/edit/DuTFYbLVbPkCIvRznYjG?p=preview

其中ng-pattern regEx不适用于输入文本字段。

只需要正确应用所需的验证。

HTML:

<body ng-controller="tableController">
    <form name="test">
        <div ng-repeat="model in models">
            <span ng-bind="model.caption"></span>
            <div ng-form name="frm{{$index}}">
                <input name="input{{$index}}"
                    ng-model="data[model.name]"
                    ng-disabled="model.isDisabled"
                    minlength="{{model.minlength}}"
                    maxlength="{{model.maxlength}}"
                    ng-show="model.isShow"
                    ng-pattern="model.pattern"
                    ng-required="true" />
                <br />
                {{data[model.name]}}
            </div>
        </div>
    </form>
    <br />
    <br />
</body>

JS:

angular.module("app", []).controller("tableController", function ($scope) {
        $scope.regEx = "/^\\d+$/";
        $scope.data = {};
        $scope.data.one = 234;
        $scope.data.two = 32432;
        $scope.models = [
            { name: "one", caption: "cOne", isDisabled: false, isShow: true, minlength: 2, maxlength: 10, pattern: "/^\d+$/" },
            { name: "two", caption: "cTwo", isDisabled: false, isShow: true, minlength: 2, maxlength: 5, pattern: "/^\d+$/" },
        ];
            });

有什么建议吗?

-Thanks

1 个答案:

答案 0 :(得分:12)

将正则表达式分配更改为以下内容。因为它应该是正则表达式。如果你用双引号提到那么它就变成了一个字符串。

$scope.regEx = /^\d+$/;