AngularJS ng-pattern not working

Please take a look at the plunker.

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

where ng-pattern regEx will not apply to the input text field.

where only the correct check is required.

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

+6
source share
1 answer

Change the regex assignment to the one below. Since it should be a regular expression. If you specify in double quotes, this will become a string.

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

All Articles