2012-10-04 101 views
0

我有一个文本输入字段,我想检查用户放在那里。我想让他们把金钱与美元或美元。他们应该可以输入:正则表达式来检查文本框中的字符串

$12 
$12.0 
12 
12.0 
$ 12.0 

不知道我该怎么做。我认为正则表达式可能会有帮助,但我不知道如何做到这一点。

+0

不是这是如何关系到了jQuery UI的标签?也许你的意思是jquery-validate? – Barmar

回答

2

基本正则表达式。

(/^\$?\s?\d+(\.\d+)?$/).test("$12.0"); 


/-Beginning of Reg Exp 
^ - Start of line 
\$? - Match a $ or not 
\s? - Match a space or not 
\d+ - match one or more numbers 
(\.\d+)? - match a decimal point followed by more numbers 
$ - match end of a line 
/- end of reg expression 

这将失败

12..0

,因为他们在可能的解决方案

+0

如果你想允许'12.'和'.10',请参阅我对这个问题的回答(http://stackoverflow.com/questions/12724801/contains-only-digit-and-comma-or-dot -in-C-锋利/ 12724908#12724908)。但如果是钱,你不应该在小数点后需要2位数字吗? – Barmar

相关问题