Regex Currency Format - javascript

Currently, I am using the following regular expression to validate currency in html input fields:

/[1-9]\d*(?:\.\d{0,2})?/ 

Suppose this allows the following value: 13000.234.12

This value is not valid. Here are the valid values ​​that I want to resolve through:

REALLY

 125 1.25 1000.15 700.1 80.45 0.25 

INVALID

 130.1.4 21.......14 

It seems that for this there should be a standard pattern of regular expression, thoughts?

Side note . I prevent alphanumeric and dollar signs through the event event listener, so they can no longer be entered, which should alleviate this problem. p>

+4
source share
3 answers

Something like this should work:

 ^(\d*\.\d{1,2}|\d+)$ 

It corresponds to:

 1.00 1 0.23 0.2 .2 

It does not match:

 . 1.1. 
+13
source
 /^(\d*?)(\.\d{1,2})?$/ 

So this (beginning) (any number of numbers only , even zero), (., And then only numbers , one or two, should not be there though) End

+1
source

I used a comma for the decimal separator. Here are my friends:

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

All Articles