angularjs - angular restriction to not allow spaces in text field -


i not want user enter spaces in text field. don't want on submit validation rather - space not show on text field when click it.

<input ng-model="field" ng-trim="false" ng-change="field = field.split(' ').join('')" type="text"> 

update: improve code quality can create custom directive instead. don't forget directive should prevent input not keyboard, pasting.

<input type="text" ng-trim="false" ng-model="myvalue" restrict-field="myvalue"> 

here important add ng-trim="false" attribute disable trimming of input.

angular   .module('app')   .directive('restrictfield', function () {     return {         restrict: 'ae',         scope: {             restrictfield: '='         },         link: function (scope) {           // match spaces, tabs, line feeds etc           // can change regex want           var regex = /\s/g;            scope.$watch('restrictfield', function (newvalue, oldvalue) {               if (newvalue != oldvalue && regex.test(newvalue)) {                 scope.restrictfield = newvalue.replace(regex, '');               }           });         }     };   }); 

Comments

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -