0

我有一个文本输入字段,我想检查用户在那里输入的内容。我想允许他们在有或没有 $ 的情况下输入货币金额。他们应该能够输入:

$12
$12.0
12
12.0
$ 12.0

不知道我该怎么做。我认为正则表达式可能会有所帮助,但我不知道该怎么做。

4

1 回答 1

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

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

于 2012-10-04T18:47:36.153 回答