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
Post a Comment