Angular js Ng-pattern does not allow update the model

703 Views Asked by At

i have to validate the text box which shows the error message when user enters 4 digit as 9999.so i have used the ng-pattern method to show error message but ng-pattern does n't allow to update the object.my code is given below:

  <md-input-container class="" style="margin:5px 0px; margin-right:15px;">

      <input type="password" name="numCode" ng-model="datas.part[1].Value" ng-value="datas.part[1].Value" maxlength="4" ng-pattern="/(?!9{4})\d{4}/" ng-keydown="vm.testCode(datas.part[1].Value)" class="ng-pristine ng-valid md-input ng-touched"  aria-invalid="false" style="" autocomplete="off" required>
      <div ng-messages="vm.formName.numCode.$error" class="allign-padding-bottom" role="alert">
           <div ng-message-exp="['minlength','maxlength','pattern']">
                        {{::'testcode'|translate}}
            </div>
       </div>
    </md-input-container>

 vm.testCode= function (val) {
            console.info("sdf",val);
            vm.showConfirmUserCode = true;
            ///vm.isDeviceEnabled = false;
        }

In ng-keydown method the model did n't update.it shows undefined in vm.formName.numCode.$viewvalue.kindly help me to sort out this problem & check my ng-pattern.if i remove my ng-pattern means i have the updated model

1

There are 1 best solutions below

0
On

Please check this plunker: http://plnkr.co/edit/g0wsbFnfi6TEW76orxIJ?p=preview

 <body ng-controller="MainCtrl">
    <p>Hello {{name}}!</p>

    <form name="form1" novalidate>

  <input type="password" name="numCode" ng-model="inputText" ng-value="inputText" ng-pattern="/(?!9{4})\d{4}/" ng-maxlength="4" ng-keydown="testCode(inputText)" aria-invalid="false" style="" autocomplete="off" required>
  <div ng-messages="form1.numCode.$error" role="alert">
           <div ng-message="pattern">pattern error</div>
            <div ng-message="maxlength">maxlength error</div>
             <div ng-message="required">required error</div>
   </div>
</form>
  </body>

Have you referenced angular-messages as a seperate library? The ng-keydown works if its a valid text and the error messages are shown correctly.