[英]Validate an HTML number input
如何验证必须包含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
必须是整数和2个小数位
任何帮助深表感谢!
/^[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}`); }); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.