只允许使用javascript和mootools在字段中输入int和float
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
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 可以't让我的if语句处理js中的html表单输入
- 名称输入的索引
- 如何编写HTML输入的JS内联
- 要求输入在数据列表中
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 让文本输入幻灯片显示输入时的新文本输入?然后向后滑动
- 如何将输入(type=text)从html表单传递到javascript函数
- 单击jquery清除输入值
- 而循环只设置php中输入字段中的第一个值
- 只允许使用javascript和mootools在字段中输入int和float
- Int 输入验证 - 解析服务器云代码
- 如何将字符串转换为来自按钮输入的 Int
- 如何确定为`Int,VarChar,Date,Time..`输入的模式等
- Regexp用于检查输入是否仅为整数(int),以及检查另一个输入是否仅为带有2位小数的数字
- 当用户输入输入值时,如何使用jquery或javascript将int转换为double
- 如何限制用户只输入INT
- 如何从DB获取ID,如果用户输入INT使用Ajax/PHP文本输入
- 输入数字验证-限制只输入数字或数字,int &浮动
- 如何在javascript中获得int变量以在java方法中输入