dip
dip

Reputation: 1261

In AngularJS, howto use ng-trim=false with input type as email

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.

Upvotes: 1

Views: 18787

Answers (1)

user3458948
user3458948

Reputation: 49

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

Upvotes: 3

Related Questions