8

有没有办法在 angularjs 中实现条件 ng-pattern

ng-pattern="(myForm.exipration.$dirty ? ('/^\d{2}[/]\d{4}$/') : '')"

我像上面一样尝试过,但没有帮助。

4

2 回答 2

17

标记

<input ... ng-pattern="handlePatternPassword">

控制器

$scope.handlePatternPassword = (function() {
  var regex = /^[A-Za-z0-9!@#$%^&*()_]{4,20}$/;
  return {
    test: function(value) {
      if ($scope.user.isLogged) {
        return (value.length > 0) ? regex.test(value) : true;
      } else {
        return regex.test(value);
      }
    }
  };
})();

来自https://stackoverflow.com/a/18984874/4640499

于 2015-11-20T17:00:37.883 回答
10

以下对我有用(AngularJS v1.5.11):

ng-pattern="condition ? '[A-Za-z0-9_-]{1,60}' : ''"
于 2017-06-27T08:41:55.577 回答