jith10
jith10

Reputation: 553

How to restrict a character entry in a textbox in angular javascript

hi i have a textbox which should reject the entry of a decimal point. when a user enters a "." then the text box should not make that entry in the text box.

will any ng-functions help ? or a seperate directive needs to be written ?

Upvotes: 0

Views: 396

Answers (2)

Cs 8128
Cs 8128

Reputation: 113

angular.module('app', [])
  .controller('myCtrl', function($scope) {
    $scope.textinput = '';
  })
  .directive('noPeriod', function() {
    return {
      require: 'ngModel',
      restrict: 'A',
      link: function(scope, element, attrs, modelCtrl) {
        modelCtrl.$parsers.push(function(inputValue) {
          if (inputValue == undefined)
            return ''
          cleanInputValue = inputValue.replace('.', '');
          if (cleanInputValue != inputValue) {
            modelCtrl.$setViewValue(cleanInputValue);
            modelCtrl.$render();
          }
          return cleanInputValue;
        });
      }
    }
  });

Upvotes: 1

ziaulain
ziaulain

Reputation: 94

use ng-change. Get the hexa of the . and remove it from string...

Upvotes: 0

Related Questions