0

我正在 gwt 中进行验证。我有只允许整数和十进制值的文本框。我使用了 regex 模式,比如^[0-9.]+$.It 工作正常。但是当我输入单点时,.它接受了。如何在上述正则表达式模式上限制单个点?

4

3 回答 3

0

对于我们的 GWT 项目,我们使用 Hibernate 验证器和注释,例如

@DecimalMax(value = "9999999.99")
@DecimalMin(value = "0.01")
private BigDecimal amount;

而不是正则表达式。

于 2013-09-20T10:48:30.517 回答
0
^[0-9]+([.][0-9]+)?$

所以一系列数字,可选地后跟一个句点和一系列数字。

于 2013-09-20T10:03:19.330 回答
0

怎么样:

^\d+(?:\.\d+)?$

它将匹配整数或十进制数。

解释:

The regular expression:

(?-imsx:^\d+(?:\.\d+)?$)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))
----------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
----------------------------------------------------------------------
    \.                       '.'
----------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
----------------------------------------------------------------------
  )?                       end of grouping
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
于 2013-09-20T10:03:38.530 回答