如何验证必须包含 1.00 或更大值的字段?还必须包含 2 位小数。
我发现了这个......
<input pattern="\d?\d\.\d\d" maxlength=5 size=5 onchange="check(this)">
<script>
function check(elem) {
if(!elem.value.match(/^\d?\d\.\d\d$/)) {
alert('Error in data – use the format dd.dd (d = digit)');
}
}
</script>
但它不允许超过 99.99 的值。
抱歉,我需要添加更多详细信息来解释这一点。
我需要接受 1.00 或 10.00 甚至 1000.00
只是没有
1 或 10 或 2.0 或 20.0
必须是整数和小数点后两位
非常感谢任何帮助!
最佳答案
/^[1-9]+\d*\.\d{2}$/g
应该在哪里工作
^[1-9] : makes sure the non-decimal part is greater than 0.
\d* : allows trailing zeroes as in 1000
const regex = /^[1-9]+\d*\.\d{2}$/g;
const str = `1601.91`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
关于javascript - 验证 HTML 数字输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44642804/