Conditional ng-pattern in angular js

心不动则不痛 提交于 2019-12-07 01:26:28

问题


Is there a way to achieve conditional ng-pattern in angularjs

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

i tried like above, but does not help.


回答1:


Markup

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

Controller

$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);
      }
    }
  };
})();

From https://stackoverflow.com/a/18984874/4640499.




回答2:


The following worked for me (AngularJS v1.5.11):

ng-pattern="condition ? '[A-Za-z0-9_-]{1,60}' : ''"


来源:https://stackoverflow.com/questions/33832062/conditional-ng-pattern-in-angular-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!