Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
88
rated 0 times [  95] [ 7]  / answers: 1 / hits: 33809  / 12 Years ago, fri, august 3, 2012, 12:00:00

Been out of the regex game for a while. Trying to come up with something that will allow the user to enter a money value either with/without dollar sign or with/without commas. For example, all the of the following values should be valid:



5
5.1
5.10
$5
500,000
500,000.1
500,000.10
$100,000,000.50
etc....


Could someone please help me out?


More From » regex

 Answers
52

This should work:



isValid = str.search(/^$?[d,]+(.d*)?$/) >= 0;


A little more strict with comma placement (would reject 3,2.10, for example):



isValid = str.search(/^$?d+(,d{3})*(.d*)?$/) >= 0;


To get a number out of it:



if(isValid) {
var num = Number(str.replace(/[$,]/g, ''));
...
}

[#83872] Thursday, August 2, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
demondp

Total Points: 154
Total Questions: 97
Total Answers: 99

Location: Mali
Member since Thu, Jul 9, 2020
4 Years ago
;