March 25, 2016

Remove space in text field using angular.js

Removing space in text field using Angular.js is easy. We can do this by ng-change attribute.

I will show you how to do this in two different ways.

Way 1

Put ng-change attribute to the input field to which you want to the remove white-space.

Say if the ng-model is contact.name, then ng-change would be contact.name = contact.name.split(' ').join('')

HTML

<input type="text" ng-model="contact.name" ng-change="contact.name = contact.name.split(' ').join('')" />

Here it splits the white space and joins it. It's simple right.

Way 2

In this, instead of adding values of ng-change directly to the input we will define it through a function.

HTML

<input type="text" ng-model="contact.name" ng-change="noSpace()" />

Angular

$scope.noSpace = function() {
   $scope.contact.name = $scope.contact.name.split(' ').join('');
};

Note:

As your using angular don't forget to import angular.js file.

demo

No comments:

Post a Comment

Popular Posts

Views