首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将regex表达式用于验证介于1.0到4.5之间的数字(十进制值之间的范围)?

如何将regex表达式用于验证介于1.0到4.5之间的数字(十进制值之间的范围)?
EN

Stack Overflow用户
提问于 2022-03-22 04:47:02
回答 3查看 386关注 0票数 0

我需要一个表达式来验证1.0到4.5之间的数字,但它并不是精确的。

表达式I使用:/^1-4{0,1}(?:.d{1,2})?$/

要求只接受1.0到4.5之间的值

代码语言:javascript
复制
buildInserForm(): void {
    this.insertLeavesForm = this.fb.group({
     **your text**
      hours: [
        { value: 4.5, disabled: true },
        [
          Validators.required,
          Validators.pattern(**/^[1-4]{0,1}(?:[.]\d{1,2})?$/**),
        ],
      ],
    });
  }

目前将数字限制在1.0到4.0之间,但问题出现在小数点上,如果在小数位中输入任何介于6-9之间的数字,如1.7、2.8、3.9,则会显示错误。

验收标准为1.0至4.5。

这个图像显示值被输入到多个十进制位,这是错误的,

只需要一个小数位值。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-03-22 06:48:37

这是我创建的正则表达式。pattern(/^1-3{0,1}(?:.{0,1})?4{0,1}(?:.{0,1})?$/)

解释

代码语言:javascript
复制
/^[1-3]{0,1}(?:[.][0-9]{0,1})?[4]{0,1}(?:[.][0-5]{0,1})?$/


/^            start of the input
[1-3]         First input to be taken between
{0,1}         This shows how many times it can be repeated
(             Parenthesis shows next entered digit is optional
?             Question mark is for using digit zero or once
:[.]          This is for what the next Character would be eg ".", "@"
[0-9]         input to be taken between.
{0,1}         This shows how many times it can be repeated.
)             Option part closes
?             Question mark is for using digit zero or once.
[4]           This shows what can be other input that can be taken
{0,1}         How many times it can be use , SO in this case 0 or 1 times


(?:[.]       There after again same option part  with decimal point

             decimal value of 4and its limitation for 0 to 5
[0-5]        This is to set limitation 0-5 as per our requirement
{0,1}        Its existence 0 or 1 times
)            Close of optional part.
?            Thereafter showing the existence of an optional part once or twice.
$/           Shows to match expression after it.
票数 -1
EN

Stack Overflow用户

发布于 2022-03-22 05:08:41

Regex很难检查数字范围。它应该考虑非十进制数吗?如果有一个以上的小数点呢?如果你想增加/减少范围怎么办?下面是关于这个主题的更多信息:Regular expression Range with decimal 0.1 - 7.0

我建议使用简单的min/max验证器。此外,这还可以让您控制用户值是否低于或高于标准,例如,允许您适当地显示自定义错误消息。而regex将简单地计算为true/false。

代码语言:javascript
复制
[
 Validators.required,
 Validators.min(1),
 Validators.max(4.5),
 Validators.maxLength(3)
]
票数 2
EN

Stack Overflow用户

发布于 2022-03-22 05:03:16

您可以使用以下regex模式:

代码语言:javascript
复制
^(?:[1-3](?:\.\d{1,2})?|4(?:\.(?:[0-4][0-9]|50?)?))$

Demo

这个正则表达式表示匹配:

代码语言:javascript
复制
^                            start of the input
(?:
    [1-3]                    match 1-3
    (?:\.\d{1,2})?           followed by optional decimal and 1 or 2 digits
    |                        OR
    4                        match 4
    (?:
        \.                   decimal point
        (?:[0-4][0-9]|50?)?  match 4.00 to 4.49 or 4.5 or 4.50
    )
)
$                            end of the input
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71567037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档