0

I am trying to implement custom validation with angular js.

The following code runs perfectly on FireFox however IE 11 throws an error

Expected ':'

for the line return scope.valFunc({value});

Any ideas how to remedy that for IE? Thanks.

Directive:

crudApp.directive('customValidationFunction', function()  {
    return {
        restrict: 'A',
        require: 'ngModel',
        scope: {
            valFunc: '&customValidationFunction'
        },
        link: function(scope, element, attrs, controller)  {
            const normalizedFunc = function(modelValue, viewValue) {
                const $value = modelValue || viewValue;
                return scope.valFunc({$value});
            };
            controller.$validators.customValidationFunction = normalizedFunc;
        }
    };
});

Validation Function:

 //custom validation test 
    $scope.custValidationFunc = function($value) {
        if (!$value) {
            return true;
        }
        const lowVal = String($value).toLowerCase();
        return lowVal.indexOf("arnold") === -1 && lowVal.indexOf("sylvester") === -1;
    }

Html:

<input type="text" class="form-control" id="i_position" name="i_position" aria-describedby="i_position_help" placeholder="Enter your Position" ng-model="position"  custom-validation-function="custValidationFunc($value)">

1 Answer 1

1

scope.valFunc({value}) is ES6 syntax & IE doesn't support it. You need to integrate Babel or simple change to scope.valFunc({value: value})

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

1 Comment

Ok thanks - it was actually scope.valFunc({$value: $value}) that worked!

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.