ngPattern is not working after upgrading Angular JS 1.2.x to 1.4.x

153 Views Asked by At

The pattern validation using angular ngPattern is not working after upgrading the Angular JS library to 1.4.1. See the code below.

    <form class="form-horizontal" name="someForm" role="form" novalidate>
        <div>field1Value = '{{field1Value}}'</div>
        <p class="input-group" ng-class="{ 'has-error': someForm.field1.$invalid }">
          <input id="date" name="field1" type="text" 
            ng-model="field1Value" class="form-control" ng-pattern="{{expr1}}" ng-trim="false"  ng-maxlength="10" ng-required="false" ng-disabled="false" />
        </p>
    </form>

See the Plunker

Thanks in advance..

1

There are 1 best solutions below

0
On

Finally I make it worked with the help of custom directive. I am still doubt full why the ngPattern have no inbuilt support. I don't know I make any mistake in my side.

Here is the custom directive.

app.directive('fieldPattern',function() {
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function(scope,elem,attrs,ngModelCtrl) {
      var dRegex = new RegExp(attrs.fieldPattern);
      ngModelCtrl.$parsers.unshift(function(value) {
        if (typeof value === 'string') {
          var isValid = dRegex.test(value);
          ngModelCtrl.$setValidity(attrs.name,isValid);
          if (!isValid) {
            return undefined;
          }
        }
        return value;
      });
    }
  };
});

See the plunker