6

Please take a look plunker.

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

where ng-pattern regEx is not going to apply on input text field.

where only required validation is applying properly.

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+$/" },
        ];
            });

Any suggestion ??

-Thanks

user3249448
  • 1,369
  • 2
  • 14
  • 34

1 Answers1

12

Change the regular expression assignment to the below. Since it should be a regular expression. If you mention in double quotes then it becomes a string.

$scope.regEx = /^\d+$/;
Sai
  • 2,068
  • 19
  • 24
  • 1
    Hi, what about if i am going to use pattern from model... it still have same issue.. http://plnkr.co/edit/bkokVe?p=preview plz check above plunker link – user3249448 Mar 24 '14 at 15:49
  • I still would say the same thing to do. Please check this [plunkr](http://plnkr.co/edit/MImCWigmi4CC1WWc0EZM?p=preview) – Sai Mar 24 '14 at 18:00
  • and what if in model we have only string passed by user instead of regexp object ? – bunny1985 Aug 22 '14 at 07:18