2

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{{campo.id}}.$dirty && datosBaseForm.campo{{campo.id}}.$invalid]"> ' +
                    '<span class="fa fa-check" aria-hidden="true" data-ng-show="datosBaseForm.campo{{campo.id}}.$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="{{campo.id}}" data-ng-model="campo.Valor" data-ng-model-options="{ updateOn: \'blur\' }" name="campo{{campo.id}}" type="{{campo.tipo}}" required="" data-ng-pattern="{{campo.validacion}}" placeholder="{{campo.marcador}}">' +                            
                        '<div data-ng-show="datosBaseForm.$submitted || datosBaseForm.campo{{campo.id}}.$touched">' +
                            '<span data-ng-show="datosBaseForm.campo{{campo.id}}.$error.required" class="errorText">El {{campo.nombre}} es obligatorio.</span>' +
                            '<span data-ng-show="datosBaseForm.campo{{campo.id}}.$error.{{campo.tipo}}" class="errorText">Por favor introducir un {{campo.nombre}} valido.</span>' +
                        '</div>' +
                    '</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] http://errors.angularjs.org/1.5.8/$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 https://www.debuggex.com/

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?

2
  • If you need to create a regex from a variable, you need to use a constructor notation - $scope.FormatedRegex = new RegExp(campo.validacion) Commented Apr 26, 2017 at 16:19
  • 1
    Thanks @Wiktor! scope.FormatedRegex = new RegExp(campo.validacion); This returns an Object, I then had to reference the "source" in the object for it to work. $scope.FormatedRegex = new RegExp(campo.validacion); campo.validacion = $scope.FormatedRegex.source; Commented Apr 27, 2017 at 9:33

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.