简体   繁体   English

数字和一位小数的正则表达式

[英]Regular expression for numbers and one decimal

I can't seem to get a simple regular expression to work.我似乎无法使用简单的正则表达式。 Here's what I have at the moment:这是我目前所拥有的:

$(".Hours").on('input', function (e) {

    var regex = /^\d+(\.\d{0,2})?$/g;

    if (!regex.test(this.value)) {
        if (!regex.test(this.value[0]))
            this.value = this.value.substring(1, this.value.length);
        else
            this.value = this.value.substring(0, this.value.length - 1);
    }
});

I need the user to be able to only enter numbers and one decimal (with only two numbers after the decimal).我需要用户只能输入数字和一位小数(小数点后只有两个数字)。 It's working properly now, with the exception that a user cannot start with a decimal.它现在工作正常,除了用户不能以小数开头。

Acceptable:可接受:

23.53
0.43
1111.43
54335.34
235.23
.53 <--- Not working 

Unacceptable:不可接受:

0234.32 <--- The user can currently do this
23.453
1.343
.234.23
1.453.23

Any help on this?这有什么帮助吗?

Updated answer:更新的答案:

RegExp -正则表达式 -

^(?:0|[1-9]\d+|)?(?:.?\d{0,2})?$

Explanation at regex101 : regex101处的说明

在此处输入图片说明

Original answer:原答案:

fiddle Demo小提琴演示

RegExp -正则表达式 -

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

Explanation说明

Assert position at the beginning of the string «^»
Match the regular expression below and capture its match into backreference number 1 «(\d+)?»
   Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match a single digit 0..9 «\d+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the regular expression below and capture its match into backreference number 2 «([.]?\d{0,2})?»
   Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match the character “.” «[.]?»
      Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match a single digit 0..9 «\d{0,2}»
      Between zero and 2 times, as many times as possible, giving back as needed (greedy) «{0,2}»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»

Here is a suggestion : /^((\\d|[1-9]\\d+)(\\.\\d{1,2})?|\\.\\d{1,2})$/ .这是一个建议: /^((\\d|[1-9]\\d+)(\\.\\d{1,2})?|\\.\\d{1,2})$/

Allows : 0 , 0.00 , 100 , 100.1 , 100.10 , .1 , .10 ...允许: 0 , 0.00 , 100 , 100.1 , 100.10 , .1 , .10 ...

Rejects : 01 , 01.1 , 100. , .100 , .拒绝: 01 , 01.1 , 100. , .100 , . ... ...

这是否满足您的需求:

var regex = /^\d+([.]?\d{0,2})?$/g;

Your regex: var regex = /^\\d+(\\.\\d{0,2})?$/g;你的正则表达式: var regex = /^\\d+(\\.\\d{0,2})?$/g;

What you need: var regex = /^\\d*(\\.\\d{1,2})?$/;你需要什么: var regex = /^\\d*(\\.\\d{1,2})?$/;

You were requiring at least one digit before the decimal ( \\d+ ).您要求小数点前至少有一位数字 ( \\d+ )。 I have also changed it so if you include a decimal, there must be at least one digit after it.我也改变了它,所以如果你包含一个小数,它后面必须至少有一个数字。

This one will force you to add digit after decimal separator :这将迫使您在小数点分隔符后添加数字:

^\d+([.]\d)?$

Example :示例:

  • 123 => true 123 => 真
    1. => false => 假
  • 123.1 => true 123.1 => 真
  • 123.12 => false 123.12 => 假
  • 123.. => false 123.. => 假

if you want more digit;如果你想要更多的数字; 3 for ex; 3 为前; make sure you fix min to "1" for digit after dicimal确保将小数后的数字的 min 固定为“1”

^\d+([.]\d{1,3)?$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM