如何使用正则表达式来验证 1.0 到 4.5 之间的数字(十进制值之间的范围)?
How to put regex expression to validate number between 1.0 to 4.5 (range between decimal values)?
我需要一个表达式来验证 1.0 到 4.5 之间的数字,但它不能准确地工作。
我使用的表达式:/^[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。
此图显示输入的值有多个小数位,这是错误的,
只需要一位小数。
您可以使用以下正则表达式模式:
^(?:[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 input
正则表达式很难检查数字范围。它应该考虑非十进制数吗?如果多于一位小数怎么办?如果您想要 increase/decrease 范围怎么办?以下是关于该主题的更多信息:Regular expression Range with decimal 0.1 - 7.0
我建议使用简单的 min/max
验证器。此外,这还可以让您控制用户值是低于还是高于标准,例如,让您适当地显示自定义错误消息。而正则表达式将简单地评估为 true/false.
[
Validators.required,
Validators.min(1),
Validators.max(4.5),
Validators.maxLength(3)
]
这是我创建的正则表达式。
模式(/^[1-3]{0,1}(?:[.][0-9]{0,1})?[4]{0,1}(?:[. ][0-5]{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.
我需要一个表达式来验证 1.0 到 4.5 之间的数字,但它不能准确地工作。
我使用的表达式:/^[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。
此图显示输入的值有多个小数位,这是错误的,
只需要一位小数。
您可以使用以下正则表达式模式:
^(?:[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 input
正则表达式很难检查数字范围。它应该考虑非十进制数吗?如果多于一位小数怎么办?如果您想要 increase/decrease 范围怎么办?以下是关于该主题的更多信息:Regular expression Range with decimal 0.1 - 7.0
我建议使用简单的 min/max
验证器。此外,这还可以让您控制用户值是低于还是高于标准,例如,让您适当地显示自定义错误消息。而正则表达式将简单地评估为 true/false.
[
Validators.required,
Validators.min(1),
Validators.max(4.5),
Validators.maxLength(3)
]
这是我创建的正则表达式。 模式(/^[1-3]{0,1}(?:[.][0-9]{0,1})?[4]{0,1}(?:[. ][0-5]{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.