I'm terrible with regular expressions but I was wondering if it was possible to use ng-pattern with a variable
For example,
ng-pattern="/^{{validationCode}}$/"
where validationCode is a variable attached to $scope in a controller
// Inside Controller
$scope.validationCode = 'response returned from server'
If
$scope.validationCode = 1234123412351234
then the ng-pattern would be
ng-pattern="/^1234123412351234$/"
But this isn't working and it seems like I need to create a custom directive which I don't really want
ng-pattern
expects a regex expression.
From Angular's documentation about ng-pattern
:
Sets pattern validation error key if the value does not match the
RegExp
pattern expression. Expected value is/regexp/
for inline patterns orregexp
for patterns defined as scope expressions.
In other words, you could create a RegExp in the controller:
$scope.pattern = new RegExp(patternFromServer);
and use it:
<input ng-model="foo" ng-pattern="pattern">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With