Javascript 正则表达式以选中文本框中的字符串

Javascript 正则表达式以选中文本框中的字符串,javascript,jquery,jquery-ui,Javascript,Jquery,Jquery Ui,我有一个文本输入字段,我想检查用户输入的内容。我想让他们把一个货币金额与美元或与美元。他们应该能够进入: $12 $12.0 12 12.0 $ 12.0 不知道我该怎么做。我认为正则表达式可能会有所帮助,但我不确定如何做到这一点。基本正则表达式 (/^\$?\s?\d+(\.\d+)?$/).test("$12.0"); / -Beginning of Reg Exp ^ - Start of line \$? - Match a $ or not \s? - Match a space

我有一个文本输入字段,我想检查用户输入的内容。我想让他们把一个货币金额与美元或与美元。他们应该能够进入:

$12
$12.0
12
12.0
$ 12.0

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

基本正则表达式

(/^\$?\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


因为它们不在您可能的解决方案中

这与jquery ui标记有什么关系?也许您的意思是jquery验证?如果您想允许
12.
.10
,请参阅我对的回答。但是如果是钱,你不应该要求小数点后正好有2位吗?