javascript - Allow null values- input type number (angularjs) - Stack Overflow

I have the following code. <input ng-keypress="onlyNumbers($event)" min="0" type

I have the following code.

<input ng-keypress="onlyNumbers($event)" min="0" type="number" step="1" ng-pattern="/^[0-9]{1,8}$/" ng-model="... >

Now it accepts only numerical values. I want null values to be accepted too. Since there are null values in database, i want it to reflect in the UI. currently i get [Object][Object] at the input for null value.

$scope.onlyNumbers = function(event){   
  var keys = {
    'up': 38,
    // Other key codes
    ...
    '9':57
  };

  for(var index in keys) {
    if (!keys.hasOwnProperty(index)) continue;
    if (event.charCode==keys[index]||event.keyCode==keys[index]) {
      return; //default event
    }
  }

  event.preventDefault();
};

Can anyone let me know how to make it work ?

I have the following code.

<input ng-keypress="onlyNumbers($event)" min="0" type="number" step="1" ng-pattern="/^[0-9]{1,8}$/" ng-model="... >

Now it accepts only numerical values. I want null values to be accepted too. Since there are null values in database, i want it to reflect in the UI. currently i get [Object][Object] at the input for null value.

$scope.onlyNumbers = function(event){   
  var keys = {
    'up': 38,
    // Other key codes
    ...
    '9':57
  };

  for(var index in keys) {
    if (!keys.hasOwnProperty(index)) continue;
    if (event.charCode==keys[index]||event.keyCode==keys[index]) {
      return; //default event
    }
  }

  event.preventDefault();
};

Can anyone let me know how to make it work ?

Share Improve this question edited Nov 23, 2022 at 5:13 Yevgeniy Afanasyev 41.5k30 gold badges186 silver badges207 bronze badges asked Jun 7, 2016 at 19:14 PatrickPatrick 4,3186 gold badges22 silver badges33 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

What if you remove the min attribute and modified the ng-pattern to check for an empty input?

<input ng-keypress="onlyNumbers($event)" type="number" step="1" ng-pattern="/^[0-9]{1,8}$|^$/" ng-model="... >

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744843715a4596709.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信