最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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="... >
发布评论

评论列表(0)

  1. 暂无评论