0

正则表达式。不是我的力量。这是我的代码行:

var regex = new RegExp("^[(1-9)(\.)]\d*$");

现在我可以输入:1-9 和我想要的很多点。问题:我只想给用户一个准确写一个点的机会。

我怎样才能做到这一点?这是我的整个脚本:

jQuery(document).ready(function($) {
$('.bet-bit-input').keypress(function (e) {
            var regex = new RegExp("[(1-9)(\.)]\d*$");
            var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
            if (regex.test(str)) {
                return true;
            }
            e.preventDefault();
            return false;
    });
}); 

谢谢你的帮助

4

2 回答 2

0

怎么样:

^[1-9]\d*(?:\.\d+)?$

解释:

  ^                        the beginning of the string
----------------------------------------------------------------------
  [1-9]                    any character of: '1' to '9'
----------------------------------------------------------------------
  \d*                      digits (0-9) (0 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

你的正则表达式^[(1-9)(\.)]\d*$意味着:

 ^                        the beginning of the string
----------------------------------------------------------------------
  [(1-9)(\.)]              any character of: '(', '1' to '9', ')',
                           '(', '\.', ')'
----------------------------------------------------------------------
  \d*                      digits (0-9) (0 or more times (matching
                           the most amount possible))
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
于 2013-09-27T16:51:24.127 回答
0

将您的正则表达式替换为^\d+(\.\d+)?$

于 2013-09-27T14:26:44.917 回答