【问题标题】:Check for 4 numbers before decimal and 1 number after decimal检查小数点前 4 个数字和小数点后 1 个数字
【发布时间】:2016-07-01 16:02:17
【问题描述】:

我有一个输入数字字段,它应该允许最多 4 个小数点前的数字和最多 1 个小数点后的数字或最多 6 个不带小数点的数字。 例如。有效 1.2、113.5、1234.5、456789。

我在按键上使用了这个 RegEx ^\d{0,4}\.?(\.\d{0,1})?$。它工作正常,但只有在显示像 113.55 这样的数字后才会给出错误。我该如何解决这个问题?

我的按键功能:

函数 OnKeyPress(e,DivID) {

            if ( e.which != 8 && e.which != 0  && e.which != 13 && e.which != 46 && (e.which < 48 || e.which > 57)) {
                        return false;
            }

            var val = j$('[id$='+DivID+']').val();


            if(DivID == 'ProximityCPPercentage')
            {
                var x = event.which || event.keyCode;
                if(val.indexOf('.') >= 0 && e.which == 46)
                    return false;
                else if(e.which == 46 && val.length == 3)
                    return false;
                if(val.indexOf('.') == 0)
                    val = '0' + val;
                if(e.which != 46)
                {                        
                    strval = val + String.fromCharCode(x);
                    var re = /^((.|0|[1-9]\d?)(\.\d{1})?|100(\.0?)?)$/;
                    if(!re.test(strval))
                        return false;
                }
            }              
            else if(val.indexOf('.') > 0)
            {
                if(e.which == 46 )
                    return false;
                var arra = val.split('.');
                var decval = arra[1];

                var val = arra[0];
                if(val.length > 6)
                    return false;
                if(decval.length > 0)
                    return false;                        
            }                
            else if(e.which != 46 )
            {
                if(val.length > 5)

                    return false;
            }

        }

【问题讨论】:

  • 使用两个正则表达式 - /^\d{0,4}[.]\d{0,1}$/ & /\d{6}/ ?
  • ^\d{0,4}([.\d]\d)?$
  • .4 是一个有效值吗?那么4.呢?
  • 您需要更准确地定义可接受的模式。是“1”。公认? “123”是否被接受?是否接受“.3”?另外,您到底想在按键上发生什么?您想取消/忽略导致模式不匹配的按键吗?你能告诉我们你的按键处理代码吗?您是否考虑过在输入元素上使用pattern 属性?
  • 接受的值为 1,12,123,1234,12345,123456,0.1,1.1,12.1,123.1,1234.1。文本字段不应允许输入这些以外的内容。

标签: javascript regex


【解决方案1】:

使用以下正则表达式

^\d{0,4}([.\d]\d)?$

Regex explanation here


如果您不想匹配 5 位数字,请使用 negative look-ahead assertion 来避免这种情况

^(?!\d{5}$)\d{0,4}([.\d]\d)?$

Regex explanation here

【讨论】:

  • 我认为您在 [] 部分中的意思是 \. 而不是 .。所以它应该看起来像^\d{0,4}([\.\d]\d)?$
  • 这匹配12345
  • +1 如果您包含使.44. 无效的变体。目前尚不清楚 OP 是否需要,但其他人可能需要。
【解决方案2】:
/^(?:\d{0,4}\.?(\d)|\d{0,6})?$/

注意:这也匹配 .212345''(空字符串)。根据您的问题,尚不清楚您是否要排除这些。

解释:

  1. ^ 开始排队。
  2. (?: 启动“非捕获组”。
  3. \d{0,4} 0 到四位数之间。
  4. \.? 零个或一个字面点。
  5. (\d) 捕获一位数。 (你想捕捉这个吗?)
  6. |
  7. \d{0,6} 零或六位数。
  8. ) 关闭我们的非捕获组(编号 2)。
  9. $ 结束这一行。

测试:

var reg_exp = /^(?:\d{0,4}\.?(\d)|\d{0,6})?$/;
[
  '1.2',
  '113.5',
  '1234.5',
  '456789',
  '12345',
  '.2',
  '',
  '1234.',
  '113.55'
].forEach(c => {
  console.log('"' + c + '" tests to "' + reg_exp.test(c) + '"');
});

// "1.2" tests to "true"
// "113.5" tests to "true"
// "1234.5" tests to "true"
// "456789" tests to "true"
// "12345" tests to "true"
// ".2" tests to "true"
// "" tests to "true"
// "1234." tests to "false"
// "113.55" tests to "false"

【讨论】:

  • 它也匹配12345
  • 没错,我假设“或 6 个不带小数的数字”意味着“最多六位小数”,但它们可能意味着“正好六位”。如果是这样,只需将最后一个 \d{0,6} 更改为 \d{6} 即可。不过感谢您的评论。
  • 你的假设是错误的。它也匹配1234.
猜你喜欢
  • 2014-08-24
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多