验证价格使用正则表达式

huangapple go评论60阅读模式
英文:

Validating prices use regular expressions

问题

我正在尝试在Vue中使用正则表达式验证输入,但我不知道如何编写正则表达式,也找不到在线上匹配我想要做的内容的方法。

我想验证的是价格,应该是带有两位小数的浮点数,小数点前可以是1个数字或9个数字。例如:

0.50 

1.00

99999.99

999999999.00

我尝试了这个正则表达式:

v => (/\d{1,3}(?:[.,]\d{3})*(?:[.,]\d{2})/.test(v))

但不起作用。

抱歉,如果我的英文不是很好。我感谢您的帮助!

英文:

I'm trying to validate an input with regEx in Vue, which I don't have any idea how to make one and couldn't find online how to match what I want to do.

The thing is I'm trying to validate a price that should be a float with 2 decimal numbers, and it can be 1 number before the . or 9 digits. For example:

0.50 

1.00

99999.99

999999999.00

I tried this:

v => (/\d{1,3}(?:[.,]\d{3})*(?:[.,]\d{2})/.test(v))

But doesn't work.

Sorry if my english is not very good. I appreciate the help!

答案1

得分: 2

^\d{1,9}.\d{1,2}$

查看 regex101 演示

英文:

To match 1-9 digits before the dot, and 2 decimal numbers:

^\d{1,9}\.\d{1,2}$

See a regex101 demo.

答案2

得分: 1

请检查匹配整数部分从0到999999999的值,小数点后不超过2位的数字。以下是一个假设整个字符串从开头(^)到结尾($)进行检查的模板,它包括:

  • 必需的初始部分,要么是0,要么包含1到9位数字,并且不以"0"开头;

  • 可选的以"."和两位数字结尾:

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

英文:

What do you want? Check the value for matching a number from 0 to 999999999 in the integer part and no more than 2 numbers after "."?

A template assuming that the entire string being checked from the beginning (^) to the end ($) consists of

  • mandatory initial part, which is either 0 or contains from 1 to 9 digits, and does not start with "0" ;

  • optional ending of "." and two digits:

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

huangapple
  • 本文由 发表于 2023年2月19日 07:43:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定