[英]How to validate input text to ensure only floats or ints greater than a minimum value are entered?
[英]Only allowing ints and floats to be entered into field with javascript and mootools
我需要某种关于如何只允许将int或float输入到表单输入字段中的指示。 验证必须在按键事件上进行。 我遇到的问题是,例如在输入1.2
时,keyup事件函数中的检查会看到1.
这不是数字。
这是我的代码:
document.id('inputheight').addEvent('keyup', function(e) {
this.value = this.value.toFloat();
if (this.value == 'NaN') {
this.value = 0;
}
});
任何帮助表示赞赏!
您可以简单地清理keyup字段的值。 这样的事情应该可以解决问题:
this.value = this.value.replace(/([^\d.]+)?((\d*\.?\d*)(.*)?$)/, "$3");
正则表达式立即用遇到的第一个数字字符串替换该值。
([^\d.]+)? // optionally matches anything which is not
// a number or decimal point at the beginning
(\d*\.?\d*) // tentatively match any integer or float number
(.*)?$ // optionally match any character following
// the decimal number until the end of the string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.