I created a validation directive called multiple-pattern
which accepts multiple validation regular expressions:
link: function (scope, elm, attr, ctrl) {
if (!ctrl) return;
let validators = [];
attr.$observe('multiplePattern', function (patterns) {
// does not trigger after value change
var parsed = scope.$eval(patterns);
However, I am puzzled as to why the $observe
callback is not activated when the variable on the controller validationRegexps
is modified (the ng-repeat's callback is triggered when the regexp
variable changes):
$scope.regexp = '^\\S*$';
$scope.validationRegexps = {'nospaces': '^\\S*$', 'nofirstnumber': '^[A-Za-z]'};
setTimeout(function () {
$scope.$apply(function () {
$scope.regexp = '[^abc]';
$scope.validationRegexps = {'noabc': '[^abc]'};
})
}, 5000);
Usage:
<div ng-pattern="regexp" multiple-pattern="validationRegexps"></div>