简体   繁体   English

限制在AngularJS中输入的数字的长度

[英]Limit length of number entered in AngularJS

<input class="span10" type="number" max="99999" ng-maxLength="5" placeholder="Enter Points" ng-change="myFunc($index)" ng-model="myVar">

This will only change the value of form.input.$valid to false if value is larger than 99999 or 5 digits. 如果值大于99999或5位数字, form.input.$valid的值更改为false otherwise its true . 否则为true However , the user is still able to enter any length number. 但是,用户仍然可以输入任何长度数字。 I just want to limit the digits he is entering just like maxlength of html. 我只想限制他输入的数字,就像html的maxlength一样。

You will need to use the maxlength attribute as well. 您还需要使用maxlength属性。 This post explains why. 这篇文章解释了为什么。

Edit Seems Chrome is not honoring the maxlength when the input type is number, but it does for input type of text. 编辑似乎输入类型为数字时Chrome不支持maxlength,但对于文本输入类型则为maxlength。 Edge and IE do honor the maxlength when the input type is number. 输入类型为数字时,Edge和IE会遵守maxlength。 See this post for info on that. 有关此信息,请参阅此帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM