Barry Prendergast Barry Prendergast - 3 years ago 91
AngularJS Question

regex expression from data base not working in angular directive ng-pattern

I have a form in angular where the inputs (name, email, phone number, ect) are generated using an angular Directive:

.directive('camposProductos', function () {
return {
restrict: 'AEC',
template: '<div class="row" data-ng-class="{true: \'error\', false: \'good\'}[datosBaseForm.campo{{}}.$dirty && datosBaseForm.campo{{}}.$invalid]"> ' +
'<span class="fa fa-check" aria-hidden="true" data-ng-show="datosBaseForm.campo{{}}.$valid"></span>' +
'<label class="labelStyle col-md-4 col-sm-4 col-xs-12">{{campo.nombre}}:<span>*</span></label>' +
'<div class="col-md-7 col-sm-8 col-xs-12 noPaddingLat">' +
'<input class="inputBoxStyle" id="{{}}" data-ng-model="campo.Valor" data-ng-model-options="{ updateOn: \'blur\' }" name="campo{{}}" type="{{campo.tipo}}" required="" data-ng-pattern="{{campo.validacion}}" placeholder="{{campo.marcador}}">' +
'<div data-ng-show="datosBaseForm.$submitted || datosBaseForm.campo{{}}.$touched">' +
'<span data-ng-show="datosBaseForm.campo{{}}.$error.required" class="errorText">El {{campo.nombre}} es obligatorio.</span>' +
'<span data-ng-show="datosBaseForm.campo{{}}.$error.{{campo.tipo}}" class="errorText">Por favor introducir un {{campo.nombre}} valido.</span>' +
'</div>' +
'</div>' +
replace: true,
transclude: true


When I reference the regex expression for validating the inputs: data-ng-pattern="{{campo.validacion}}"
the validation works but angular throws a error:

angular.js:13920 Error: [$parse:lexerr]$parse/lexerr?p0=Unexpected%20nextharacter%20&p1=s%200-0%20%5B%5E%5D&p2=%5E((%5B%5E%3C%3E()%5C%5B%5C%5D%5C%5C.%2C%3B%3A%5Cs%40%22%5D%2B(%5C.%5B%5E%3C%3E()%5C%5B%5C%5D%5C%5C.%2C%3B%3A%5Cs%40%22%5D%2B)*)%7C(%22.%2B%22))%40((%5C%5B%5B0-9%5D%7B1%2C3%7D%5C.%5B0-9%5D%7B1%2C3%7D%5C.%5B0-9%5D%7B1%2C3%7D%5C.%5B0-9%5D%7B1%2C3%7D%5D)%7C((%5Ba-zA-Z%5C-0-9%5D%2B%5C.)%2B%5Ba-zA-Z%5D%7B2%2C%7D))%24

the expression in the data base is: ^(([^<>()[]\.,;:\s@"]+(.[^<>()[]\.,;:\s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$
this is a valid expression when I check it in

I have seen that for ng-pattern you may need to add a "/" at each end of the expression for it to work.
when I add the "/" to each end of the expression:

$scope.FormatedRegex = '/' + campo.validacion + '/';

The angular $parse:lexerr error no longer appears but the validation no longer works.

My form validates correctly when i don't format the expression but throws an error in the console for each field in the form that requires validation with a regex expression.

How can I reference the Regular expression or format the regular expression so that no error is given and the fields validate as expected?

Answer Source

Use a regex constructor notation to build a regex object from it:

$scope.FormatedRegex = new RegExp(campo.validacion);

This will result in a RegExp object that you may use further in your code.

To get the underlying pattern, you may use $scope.FormatedRegex.source.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download