How do I check the value of a currency using JavaScript?
2 answers
regex can confirm this for you:
/[\$£¥]\d+(\.\d\d)?/
(but with the euro sign there too)
this one i gave won't be the most reliable way to do it. You can change your preference. An alternative is to try something like this:
var myString = "$55.32",
currency = parseFloat(myString.replace(/^[^\d\.]*/, ''))
valid = !isNaN(currency)
;
It removes any non-numbers or characters .
from the beginning and then parses it as a float.
+2
a source to share
The problem is here three times:
- the number of currency symbols ;
- Currency has the same symbol (for example, US dollar or US dollars); and
- Differences between languages in the spelling of numbers (eg 3000.00 in English - 3000.00 in German).
My advice? Just take the numbers and forget about the currency symbol. If, however, currency detection is a whole point, then either select it separately or you will need to implement at least a subset of the above list of currencies plus the differences in locale.
+7
a source to share