我是正则表达式的新手,我想用javascript验证用户输入。
用户输入是一种货币,但我希望它没有成千上万的逗号。
有效的
"12.34"
"12,34"
"1234,5"
"123"
"123,00"
"12000"
无效的
"12a34"
"abc"
"12.000,00"
"12,000.00"
我尝试了以下正则表达式模式,但对我而言不起作用。它验证例如“ 12a34”,我不知道为什么。
/\d+([\.|\,]\d+)?/
什么是正确的正则表达式模式?您能逐步解释一下吗?
谢谢 !
不要.
在字符组中转义。尝试使用以下正则表达式:
/^\d+([.,]\d{1,2})?$/
^ = start of string
$ = end of string
()? = an optional capturegroup ( e.g. to validate "123")
{x,y} = The symbol may occur minimum x and maximum y times
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句