ng-pattern指令搞乱了输入的能力

时间:2014-11-27 09:49:03

标签: angularjs

我需要使用ng-pattern验证安全代码是否正好有4位数。添加ng-pattern="/^[0-9]{4}$/"后输入无法显示模型属性(安全代码)。如果我删除ng-pattern,那么表单和输入工作但没有验证。如何解决?

http://jsbin.com/saroyo/1/

1 个答案:

答案 0 :(得分:0)

更新时只显示一个错误



var app = angular.module('myApp',[]);
app.controller('MyCtrl',function($scope){

});

</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="MyCtrl">
<form name="myForm">
    Code: <input type="number " name="code" ng-model="user.code"
      ng-minlength="4" ng-maxlength="4">
    <span class="error" ng-show="myForm.code.$error.minlength">
      Should be 4 charachters long</span>
    <span class="error" ng-show="myForm.code.$error.maxlength">
      Should be 4 charachters long</span><br>
    <span class="error" ng-model="user.code" ng-show="(user.code === parseInt(user.code, 10)) && (myForm.$dirty === true) && (myForm.code.$error.minlength == false) && (myForm.code.$error.maxlength == false)">
      Should have only integers</span><br>
  </form>
</div>
</div>
&#13;
&#13;
&#13;