限制文本框中的单个点

时间:2013-09-20 09:53:52

标签: regex validation gwt textbox

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

3 个答案:

答案 0 :(得分:0)

^[0-9]+([.][0-9]+)?$

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

答案 1 :(得分: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
----------------------------------------------------------------------

答案 2 :(得分:0)

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

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

而不是正则表达式。