正则表达式使用Javascript验证巴西货币

时间:2021-11-12 07:36:01

I need to validate some form fileds that contain brazilian money (its name is "Real") using Javascript. It has the following format:

我需要使用Javascript验证包含巴西钱(其名称为“Real”)的一些表单文件。它具有以下格式:

            0,01
            0,12
            1,23
           12,34
          123,45
        1.234,56
       12.235,67
      123.456,78
    1.234.567,89
   12.345.678,90
  123.456.789,01
1.234.567.890,12

My regex knowledge is weak, can somebody help me please?

我的正则表达式知识很弱,有人可以帮助我吗?

2 个解决方案

#1


9  

Does this do what you want?

这样做你想要的吗?

^\d{1,3}(?:\.\d{3})*,\d{2}$

That says "1 to 3 digits, optionally followed by any number of groups of three digits preceded by a period, followed by a comma and two more digits." If you want to allow the leading whitespace present in your example, add \s* to the front:

这表示“1到3位数字,可选地后面跟着一个以句号开头的任意数量的三个数字组,后面跟一个逗号和两个以上的数字。”如果要允许示例中存在的前导空格,请将\ s *添加到前面:

^\s*\d{1,3}(?:\.\d{3})*,\d{2}$

EDIT: As @ElRonnoco pointed out, the above regular expression accepts leading zeroes (e.g. 010.100,00). To disallow those, you may use this longer version:

编辑:正如@ElRonnoco指出的那样,上面的正则表达式接受前导零(例如010.100,00)。要禁止这些,您可以使用这个更长的版本:

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

EDIT 2 The above regular expressions all match a string containing a single monetary amount and nothing else. It's not clear from the question if that's the intent.

编辑2上述正则表达式都匹配包含单个货币金额的字符串,而不是其他任何内容。从问题中不清楚这是不是意图。

EDIT 3 To allow numbers that have no decimal part, or only one decimal digit, change it like this:

编辑3要允许没有小数部分或只有一个十进制数字的数字,请更改为:

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

#2


2  

I would give this regex a try:

我会试试这个正则表达式:

\d+(?:\.\d{3})*?,\d{2}

What it says is:
- match digits until
a. a dot followed by 3 digits is found (and this step can be repeated several times)
b. or a comma followed by 2 digits is found

它的含义是: - 匹配数字直到a。找到一个点后跟3位数(此步骤可以重复几次)b。或者找到后跟2位数的逗号

EDIT:
- thanks for the comments, I forgot about the constraint for the first value
updated regex

编辑: - 感谢您的评论,我忘记了第一个值更新正则表达式的约束

\d{1,3}(?:\.\d{3})*?,\d{2}

#1


9  

Does this do what you want?

这样做你想要的吗?

^\d{1,3}(?:\.\d{3})*,\d{2}$

That says "1 to 3 digits, optionally followed by any number of groups of three digits preceded by a period, followed by a comma and two more digits." If you want to allow the leading whitespace present in your example, add \s* to the front:

这表示“1到3位数字,可选地后面跟着一个以句号开头的任意数量的三个数字组,后面跟一个逗号和两个以上的数字。”如果要允许示例中存在的前导空格,请将\ s *添加到前面:

^\s*\d{1,3}(?:\.\d{3})*,\d{2}$

EDIT: As @ElRonnoco pointed out, the above regular expression accepts leading zeroes (e.g. 010.100,00). To disallow those, you may use this longer version:

编辑:正如@ElRonnoco指出的那样,上面的正则表达式接受前导零(例如010.100,00)。要禁止这些,您可以使用这个更长的版本:

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

EDIT 2 The above regular expressions all match a string containing a single monetary amount and nothing else. It's not clear from the question if that's the intent.

编辑2上述正则表达式都匹配包含单个货币金额的字符串,而不是其他任何内容。从问题中不清楚这是不是意图。

EDIT 3 To allow numbers that have no decimal part, or only one decimal digit, change it like this:

编辑3要允许没有小数部分或只有一个十进制数字的数字,请更改为:

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

#2


2  

I would give this regex a try:

我会试试这个正则表达式:

\d+(?:\.\d{3})*?,\d{2}

What it says is:
- match digits until
a. a dot followed by 3 digits is found (and this step can be repeated several times)
b. or a comma followed by 2 digits is found

它的含义是: - 匹配数字直到a。找到一个点后跟3位数(此步骤可以重复几次)b。或者找到后跟2位数的逗号

EDIT:
- thanks for the comments, I forgot about the constraint for the first value
updated regex

编辑: - 感谢您的评论,我忘记了第一个值更新正则表达式的约束

\d{1,3}(?:\.\d{3})*?,\d{2}