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

2
  • 2
    pattern is expected a javascript regex, not a string. Commented Mar 24, 2014 at 14:43
  • Post the part of code along with Plunkr when you are facing issue in SO question. Commented Mar 24, 2014 at 14:43

1 Answer 1

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+$/;
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, what about if i am going to use pattern from model... it still have same issue.. plnkr.co/edit/bkokVe?p=preview plz check above plunker link
I still would say the same thing to do. Please check this plunkr
and what if in model we have only string passed by user instead of regexp object ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.