[英]input type = number validation in angularjs
I am trying to validate < input type = number >
by using input [number] directive of module ng of angularjs. 我试图通过使用angularjs的模块ng的输入[number]指令来验证
< input type = number >
。
When using an input of type number, with the max (or min) attribute set to number such as 使用类型编号的输入时,将max(或min)属性设置为数字,例如
<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只接受数字而且我写的指令不太好,请帮助我任何这样的目录或任何建议将不胜感激。
min
and max
are expecting a value. min
和max
期望值。 So, this should work: 所以,这应该工作:
<input type=number min="{{configRow.valueStart}}" max="{{configRow.valueEnd}}">
Here is a plunker showing a demo. 这是一个展示演示的plunker 。 (this is just a modification of the documentation demo).
(这只是对文档演示的修改)。
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
. 因此,您可以看到一个值,然后使用
parseFloat
转换为浮点值。 So interpolating the model property {{}}
to a value is required. 因此,需要将模型属性
{{}}
插值为值。
src/ng/directive/input.js SRC / NG /指令/ 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);
}
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. 最大长度不适用于
<input type="number"
我知道的最好的方法是使用oninput事件来限制maxlength,它的通用解决方案适用于所有Javascript框架。 Please see the below code. 请参阅以下代码。
<input name="somename"
oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);"
type = "number"
maxlength = "6"
/>T
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.