javascript 输入类型 = angularjs 中的数字验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20973649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:44:28  来源:igfitidea点击:

input type = number validation in angularjs

javascriptangularjs

提问by Pawan

I am trying to validate < input type = number >by using input [number] directive of module ng of angularjs.

我正在尝试< input type = number >使用 angularjs 模块 ng 的输入 [number] 指令进行验证。

When using an input of type number, with the max (or min) attribute set to number such as

当使用类型为 number 的输入时,将 max(或 min)属性设置为 number,例如

<input type=number min="20" max="40">

<input type=number min="20" max="40">

it works fine , but I my min and max data are coming data dynamically using ng-repeat such as

它工作正常,但我的最小和最大数据是使用 ng-repeat 动态传入的数据,例如

<input type=number min="configRow.valueStart" max="configRow.valueEnd">, then it is not working .

<input type=number min="configRow.valueStart" max="configRow.valueEnd">,那就不行了。

I know that min and max only accept number and I am not much good in writing directives for that , Please help me any such directory or any suggestions would be appreciated.

我知道 min 和 max 只接受数字,我不太擅长为此编写指令,请帮助我任何此类目录或任何建议,我们将不胜感激。

采纳答案by Davin Tryon

minand maxare expecting a value. So, this should work:

minmax期待价值。所以,这应该有效:

<input type=number min="{{configRow.valueStart}}" max="{{configRow.valueEnd}}">

Here is a plunkershowing a demo. (this is just a modification of the documentation demo).

这是一个展示演示的plunker。(这只是对文档演示的修改)。

At the moment, the source for these attributes looks like below. So, you can see that a value is expected that is then cast to a float with parseFloat. So interpolating the model property {{}}to a value is required.

目前,这些属性的来源如下所示。因此,您可以看到期望一个值,然后将其转换为浮点数parseFloat。因此需要将模型属性插入{{}}到一个值中。

src/ng/directive/input.js

src/ng/directive/input.js

if (attr.min) {
        var minValidator = function(value) {
          var min = parseFloat(attr.min);
          return validate(ctrl, 'min', ctrl.$isEmpty(value) || value >= min, value);
        };

        ctrl.$parsers.push(minValidator);
        ctrl.$formatters.push(minValidator);
      }

      if (attr.max) {
        var maxValidator = function(value) {
          var max = parseFloat(attr.max);
          return validate(ctrl, 'max', ctrl.$isEmpty(value) || value <= max, value);
        };

        ctrl.$parsers.push(maxValidator);
        ctrl.$formatters.push(maxValidator);
      }

回答by Neha Jain

Max length will not work with <input type="number"the best way i know is to use oninput event to limit the maxlength, its a generic solution work with all the Javascript framework. Please see the below code.

最大长度不适用于<input type="number"我所知道的最好方法是使用 oninput 事件来限制最大长度,它是适用于所有 Javascript 框架的通用解决方案。请看下面的代码。

<input name="somename"
    oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);"
    type = "number"
    maxlength = "6"
 />T