[英]Regular expression or complete javaScript to validate Salary (a 'Double' number)?
I want to validate salary text field with java script in my html page having syntax like Max six digits, a dot, max two digits after dot
and it can be done in two ways: 我想用我的html页面中的java脚本来验证薪水文本字段,该脚本具有如下语法,例如Max six digits, a dot, max two digits after dot
可以用两种方式完成:
1) I make user only enter integer values and append a dot and two zeros at the end
by some javaScript code
. 1)我让用户仅输入整数值, append a dot and two zeros at the end
通过一些javaScript code
append a dot and two zeros at the end
。
2) Check the entry given by user at time of formValidation
with a regex
that can pass the right syntax as i described above... 2)使用regex
检查用户在formValidation
时给出的条目,该regex
可以传递如上所述的正确语法...
Which will be the simpler one? 哪个更简单? And how can it be achieved? 以及如何实现呢?
Max six digits, a dot, max two digits after dot 最多六个数字,一个点,最大后两位
/^\d{1,6}(?:\.\d{0,2})?$/
It actually means between 1 and six digits, followed by optionally a dot and a max of two digits. 它实际上是指1到6位数字,后跟一个可选的点和最多两位数。
If you want to require the dot, then use 如果您需要点号,请使用
/^\d{1,6}\.\d{0,2}$/
If you want to allow commas, eg 150,000
, then try replacing \\d{1,6}
with 如果要允许逗号,例如150,000
,请尝试将\\d{1,6}
替换为
\d{1,3}(?:,?\d{3})?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.