【问题标题】:jQuery - Reg exp only 1-9 and once a time a pointjQuery - 正则表达式只有 1-9 和一次一次
【发布时间】:2013-10-03 18:10:04
【问题描述】:

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

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;
    });
}); 

感谢您的帮助

【问题讨论】:

    标签: jquery regex numbers digits


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      怎么样:

      ^[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
      

      【讨论】:

      • ^[1-9]\d*(?:\.\d+)?$ 对我不起作用。我可以输入 1-0 的数字,但没有意义。为什么? jsfiddle.net/6CJzC
      • @user2814327:BUT no point 是什么意思?你能举出一些必须通过和一些必须失败的例子吗?
      猜你喜欢
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      相关资源
      最近更新 更多