Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional ng-pattern in angular js

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.

like image 717
Hacker Avatar asked Nov 20 '15 16:11

Hacker


2 Answers

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.

like image 135
Jonatas Walker Avatar answered Nov 11 '22 17:11

Jonatas Walker


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

ng-pattern="condition ? '[A-Za-z0-9_-]{1,60}' : ''"
like image 11
Mathieu Allain Avatar answered Nov 11 '22 19:11

Mathieu Allain