Javascript regexp for all numbers and decimals

I want to do a javascript check that will accept all numeric and decimal points.

For example:
1,000,000.00 in order of
1,000,000.00 in order

1.000,000.00 not okay
1.000,000,00 not okay
1,000,000,00 is wrong
1,000,000.00 out of order

Based on what I got here:
/^[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{2})?$/only valid for 1,000,000.00, not 1,000,000.00

How can I check both formats?

Updated:

What if thousands of points are optional, for example:
1,000,000.00 in order or
1,000,000.00 in order

+3
source share
5 answers

, , (|)

var s='1,000,000.00';// tests

var result= /(^\d+([,.]\d+)?$)/.test(s) || // no thousand separator

/((^\d{1,3}(,\d{3})+(\.\d+)?)$)/.test(s) || // comma thousand separator

/((^\d{1,3}(\.\d{3})+(,\d+)?)$)/.test(s); // dot thousand separator

alert(result)

-

function validDelimNum2(s){
var rx=/(^\d+([,.]\d+)?$)|((^\d{1,3}(,\d{3})+(\.\d+)?)$)|((^\d{1,3}(\.\d{3})+(,\d+)?)$)/;
return rx.test(s);
}

//

var N= [
'10000000',
    '1,000,000.00',
    '1.000.000,00',
    '1000000.00',
    '1000000,00',
    '1.00.00',
    '1,00,00',
    '1.000,00',
    '1000,000.00'
]
var A= [];
for(var i= 0, L= N.length; i<L; i++){
    A.push(N[i]+'='+validDelimNum2(N[i]));
}
A.join('\n')

/*  returned values
10000000=true
1,000,000.00=true
1.000.000,00=true
1000000.00=true
1000000,00=true
1.00.00=false
1,00,00=false
1.000,00=true
1000,000.00=false
*/
+1

, .

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

, ,

http://regexr.com?2tves

+2

, , , 0 ( ):

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

:

^              # Start of string
[1-9]\d{0,2}   # 1-3 digits, no leading zero
(?:            # Match either...
 (?:,\d{3})*   # comma-separated triple digits
 \.\d{2}       # plus dot-separated decimals
|              # or...
 (?:\.\d{3})*  # dot-separated triple digits
 ,\d{2}        # plus comma-separated decimals
)              # End of alternation
$              # End of string
+2

( ) RE OR, :

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

UPDATE

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

, \. [,.], . 1,000.000.00 , .

It's harder to get regexp to behave like it does in JavaScript because you cannot use lookbehinds

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

/^      #anchor to the first char of string
(       #start group
  0       # 0
|       # or
  0[.,]   # 0 or 0 followed by a . or ,
  \d{2}   # 2 digits
|       # or
  [1-9]   #match 1-9
  \d{0,2} #0-2 additional digits
  (       #start group
    (,(\d{3}))* # match , and 3 digits zero or more times
    (\.\d{2})?  # match . and 2 digits zero or one
  |       # or
    (\.(\d{3})* # match . and 3 digits zero or more times
    (,\d{2})?   # match , and 2 digits zero or one time
  )       #end group
)       #end group
$/      #anchor to end of string

http://jsfiddle.net/AC3Bm/

+1
source

All Articles