[英]Regex for decimal number with optional decimal point
My requirement is to test the pasted data and if it fails then don't paste.我的要求是测试粘贴的数据,如果失败则不要粘贴。
Regex: /\d{0,4}([\.|\,]\d{0,2})?/
Data used:使用的数据:
1.2 tests true
1.2.3 test true as well
Requirement is要求是
min 0 max 4 digits before decimal point min 0 max 4 位小数点前
decimal point can be either dot or comma小数点可以是点或逗号
min 1 max 3 digits after decimal point if there exists a decimal point.小数点后最少 1 最多 3 位(如果存在小数点)。
I have tried following but does not work.我试过以下但不起作用。
Any help will be appreciated.任何帮助将不胜感激。
From your requirements从您的要求
/^\d{0,4}(?:[.,]\d{1,3})?$/
^
: Start of the line ^
: 行首\\d{0,4}
: Zero-to-four digits \\d{0,4}
:零到四位数字[.,]
: Match dot or comma [.,]
: 匹配点或逗号\\d{1,3}
: One-to-three digits \\d{1,3}
:一到三位数(?: ... )
: Non-capturing group (?: ... )
: 非捕获组(something)?
The group can occur zero or once$
: End of line $
: 行尾 input:valid { color: green; font-weight: bold } input:invalid { color: red; }
<input type="text" pattern="\\d{0,4}(?:[.,]\\d{1,3})?" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.