javascript 验证包含浮点数的文本字段是有效的百分比值吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8579267/
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
Validating a text field containing a float is a valid percentage value?
提问by Reno
I have a form text field that has a KeyUp event. On KeyUp I'm ignoring anthing but numbers, the period, backspace, delete and cursor keys. So, the only thing in the field can be a number or period.
我有一个具有 KeyUp 事件的表单文本字段。在 KeyUp 上,我忽略了数字、句点、退格、删除和光标键之外的其他东西。因此,该字段中唯一的内容可以是数字或句点。
I need to verify these things (number will become a % not over 100.00%): -The text field string is a valid number (which should always be true unless someone puts two '.'s in). -The text field string has a max of 2 decimal places -If the text field string has a decimal point, it has something after (1 or 2 decimal places, not just a period at the end) -The number is not larger than 100 (<= 100)
我需要验证这些事情(数字将成为不超过 100.00% 的 %): - 文本字段字符串是一个有效数字(除非有人输入两个“.”,否则它应该始终为真)。- 文本字段字符串最多有 2 个小数位 - 如果文本字段字符串有小数点,则后面有一些内容(1 或 2 个小数位,而不只是末尾的句点) - 数字不大于 100 (<= 100)
examples (what I need to verify):
示例(我需要验证的内容):
95 is true (valid, decimal places not required)
95. is false (don't need a '.' with no decimal place after)
95.0 is true
95.00 is true
95.000 is false (3 decimal places not valid) 100 is true
101.01 is false (over 100)
101 is false (over 100)
I've seen some things that do several pieces of that list, but I'm not smart enough to modify the regex to get exactly what I need. Thanks for any help!
我已经看到一些事情做了该列表的几部分,但我不够聪明,无法修改正则表达式以获得我需要的东西。谢谢你的帮助!
回答by Alex Turpin
Some people will suggest regexes, but I think a small function is better suited for such a validation:
有些人会建议使用正则表达式,但我认为一个小函数更适合这样的验证:
function validate(x) {
var parts = x.split(".");
if (typeof parts[1] == "string" && (parts[1].length == 0 || parts[1].length > 2))
return false;
var n = parseFloat(x);
if (isNaN(n))
return false;
if (n < 0 || n > 100)
return false;
return true;
}
console.log(validate("95"));
console.log(validate("95."));
console.log(validate("95.0"));
console.log(validate("95.00"));
console.log(validate("95.000"));
console.log(validate("101.01"));
console.log(validate("101"));
回答by Fabrizio Calderan
try this expression: the 100% is a special case of a more generic pattern
试试这个表达式:100% 是更通用模式的特例
var re = /^((0|[1-9]\d?)(\.\d{1,2})?|100(\.00?)?)$/;
explanation
解释
(0|[1-9]\d?)
allows numbers from 0 to 99
(0|[1-9]\d?)
允许从 0 到 99 的数字
(\.\d{1,2})?
allows a dot with 1 or 2 digits after
(\.\d{1,2})?
允许后面有 1 位或 2 位数字的点
|
otherwise
|
否则
100
accept 100
100
接受 100
(\.00?)?
with optional 0 or 00
(\.00?)?
可选 0 或 00
Edit:
编辑:
I tried this jsfiddle demo http://jsfiddle.net/pCDVn/1/and it's working... look at the console
我试过这个 jsfiddle 演示http://jsfiddle.net/pCDVn/1/并且它正在工作......看看控制台
回答by Briguy37
You could limit the length of your field to 5, check that the value can be cast to a number, and then verify that the number is between 0 and 100. You won't be able to enter 100.00, but why would you need to?
您可以将字段的长度限制为 5,检查该值是否可以转换为数字,然后验证该数字是否介于 0 和 100 之间。您将无法输入 100.00,但为什么需要?
Also, here'sa fiddle for testing. One note, if you really care about validating "95." as false, you'll have to add extra validation for that. However, I'd advise against this requirement, because it is still represents a valid percentage and won't affect the outcome of calculations.
另外,这里有一个用于测试的小提琴。请注意,如果您真的关心验证“95”。如果为 false,则必须为此添加额外的验证。但是,我建议反对此要求,因为它仍然代表一个有效的百分比并且不会影响计算结果。