我需要一个表达式来验证1.0到4.5之间的数字,但它并不是精确的。
表达式I使用:/^1-4{0,1}(?:.d{1,2})?$/
要求只接受1.0到4.5之间的值
但
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。

这个图像显示值被输入到多个十进制位,这是错误的,
只需要一个小数位值。
发布于 2022-03-22 06:48:37
这是我创建的正则表达式。pattern(/^1-3{0,1}(?:.{0,1})?4{0,1}(?:.{0,1})?$/)
解释
/^[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.发布于 2022-03-22 05:08:41
Regex很难检查数字范围。它应该考虑非十进制数吗?如果有一个以上的小数点呢?如果你想增加/减少范围怎么办?下面是关于这个主题的更多信息:Regular expression Range with decimal 0.1 - 7.0
我建议使用简单的min/max验证器。此外,这还可以让您控制用户值是否低于或高于标准,例如,允许您适当地显示自定义错误消息。而regex将简单地计算为true/false。
[
Validators.required,
Validators.min(1),
Validators.max(4.5),
Validators.maxLength(3)
]发布于 2022-03-22 05:03:16
您可以使用以下regex模式:
^(?:[1-3](?:\.\d{1,2})?|4(?:\.(?:[0-4][0-9]|50?)?))$
这个正则表达式表示匹配:
^ 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 inputhttps://stackoverflow.com/questions/71567037
复制相似问题