1

I am having login form with two fields ..username and password. Now by default angular trim white spaces from email field. But I want to retain white spaces so that I can show invalid email field message based on white space. How can I retain white spaces in input type email field? ( I know we can use ng-trim="false" to avoid trim but it is application in case of input type =text only ) Please help.

1 Answer 1

3

Here your directive will look like

Directive

app.directive('customValidation', function() {
    return {
        require: 'ngModel',
        link: function(scope, element, attrs, modelCtrl) {
            modelCtrl.$parsers.push(function(inputValue) {
                var transformedInput = inputValue.toLowerCase().replace(/ /g, '');
                if (transformedInput != inputValue) {
                    modelCtrl.$setViewValue(transformedInput);
                    modelCtrl.$render();
                }
                return transformedInput;
            });
        }
    };
});

HTML

<input ng-model="sth" ng-trim="false" custom-validation>

Here is Reference SO Question

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.