Checking the amount of currency using regular expressions in JavaScript

Possible duplicate:
What is a C # regex that will check for a currency, float, or integer?

How can I check the amount of currency using regular expressions in JavaScript?

Decimal separator: ,

Tens, hundreds, etc. delimiter:.

Pattern: ###.###.###,##

Examples of actual amounts:

1
1234
123456

1.234
123.456
1.234.567

1,23
12345,67
1234567,89

1.234,56
123.456,78
1.234.567,89

EDIT

I forgot to mention that the following pattern is true: ###,###,###.##

+5
source share
3 answers

Based solely on the criteria you gave, this is what I came up with.

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

http://refiddle.com/18u

, , , . , , , .

.


.

123.123,123 ( ), , . , ; , .

, , ? (^ _ ^)


:

(?:^           # beginning of string
  \d{1,3}      # one, two, or three digits
  (?:
    \.?        # optional separating period
    \d{3}      # followed by exactly three digits
  )*           # repeat this subpattern (.###) any number of times (including none at all)
  (?:,\d{2})?  # optionally followed by a decimal comma and exactly two digits
$)             # End of string.
|              # ...or...
(?:^           # beginning of string
  \d{1,3}      # one, two, or three digits
  (?:
    ,?         # optional separating comma
    \d{3}      # followed by exactly three digits
  )*           # repeat this subpattern (,###) any number of times (including none at all)
  (?:\.\d{2})? # optionally followed by a decimal perioda and exactly two digits
$)             # End of string.

, , - ?:. ( ) . ?: , . - , ?:, :

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

, regular-expressions.info - .

+16

:

/^(?:\d+(?:,\d{3})*(?:\.\d{2})?|\d+(?:\.\d{3})*(?:,\d{2})?)$/

( JavaScript):

^              # Start of string
(?:            # Match either...
 \d+           # one or more digits
 (?:,\d{3})*   # optionally followed by comma-separated threes of digits
 (?:\.\d{2})?  # optionally followed by a decimal point and exactly two digits
|              # ...or...
 \d+           # one or more digits
 (?:\.\d{3})*  # optionally followed by point-separated threes of digits
 (?:,\d{2})?   # optionally followed by a decimal comma and exactly two digits
)              # End of alternation
$              # End of string.
+7

, ( ?) 123.45:

function foo (s) { return s.match(/^\d{1,3}(?:\.?\d{3})*(?:,\d\d)?$/) }

Do you need to handle multiple separator formats?

0
source

All Articles