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.

Michael B
  • 1,660
  • 3
  • 28
  • 59
dip
  • 1,241
  • 2
  • 18
  • 32

1 Answers1

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

Community
  • 1
  • 1