Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular restriction to not allow spaces in text field

I do not want a user to enter spaces in a text field. I don't want it on submit validation but rather - a space will not show up on the text field when they click it.

like image 895
KingKongFrog Avatar asked Dec 05 '14 18:12

KingKongFrog


Video Answer


5 Answers

The selected answer is arguably not very unobtrusive. And if you need to use it in multiple places, you'll end up with duplicated code.

I prefer to prevent the input of spaces using the following directive.

app.directive('disallowSpaces', function() {
  return {
    restrict: 'A',

    link: function($scope, $element) {
      $element.bind('input', function() {
        $(this).val($(this).val().replace(/ /g, ''));
      });
    }
  };
});

This directive can be invoked like this:

<input type="text" disallow-spaces>
like image 183
Jason Swett Avatar answered Oct 19 '22 07:10

Jason Swett


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

Update: To improve code quality you can create custom directive instead. But don't forget that your directive should prevent input not only from keyboard, but also from pasting.

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

Here is important to add ng-trim="false" attribute to disable trimming of an input.

angular
  .module('app')
  .directive('restrictField', function () {
    return {
        restrict: 'AE',
        scope: {
            restrictField: '='
        },
        link: function (scope) {
          // this will match spaces, tabs, line feeds etc
          // you can change this regex as you want
          var regex = /\s/g;

          scope.$watch('restrictField', function (newValue, oldValue) {
              if (newValue != oldValue && regex.test(newValue)) {
                scope.restrictField = newValue.replace(regex, '');
              }
          });
        }
    };
  });
like image 44
Qustion Avatar answered Oct 19 '22 07:10

Qustion


If you want to achieve it without writing directive

ng-keydown="$event.keyCode != 32 ? $event:$event.preventDefault()"

like image 9
sachin bachhav Avatar answered Oct 19 '22 06:10

sachin bachhav


This works to prevent entering any special chars including spaces:

app.directive('noSpecialChar', function() {
return {
    require: 'ngModel',
    restrict: 'A',
    link: function(scope, element, attrs, modelCtrl) {
        modelCtrl.$parsers.push(function(inputValue) {
            if (inputValue == null)
                return ''
            let cleanInputValue = inputValue.replace(/[^\w]|_/gi, '');
            if (cleanInputValue != inputValue) {
                modelCtrl.$setViewValue(cleanInputValue);
                modelCtrl.$render();
            }
            return cleanInputValue;
        });
    }
}
});
like image 4
Bento Avatar answered Oct 19 '22 06:10

Bento


Use without jquery

 angular.module('app').directive('disallowSpaces', function () {
  return {
    restrict: 'A',
    require: 'ngModel',
    scope: {
      maxvalue: '=',
    },
    link: function ($scope, $element, attr, ngModelCtrl) {
      $element.bind('keydown', function () {
        function transformer(text) {
          if (text) {
            var transformedInput = text.replace(/ /g, '');
            ngModelCtrl.$setViewValue(transformedInput);
            ngModelCtrl.$render();
            return transformedInput;
          }
          return undefined;
        }
        ngModelCtrl.$parsers.push(transformer);
      });
    },
  };
});

// use disallow-spaces

<input type="text" ng-model="name" disallow-spaces />
like image 1
Saurabh Kumar Avatar answered Oct 19 '22 07:10

Saurabh Kumar