Reputation: 157
I need a regex expression that allows decimals but no commas. Such as:
1200.00
or
50.00
or
40
or
1
The expression is used in a validation to make sure people put in a price as a number, but no commas. I currently have a regex that allows for commas, numbers, and decimals, but I cannot seem to get the commas out.
This is the current regex that I have which doesnt allow commas or decimals, but I want decimals in there
/^[0-9\ ]+$/,
Thanks for your help.
Upvotes: 0
Views: 4925
Reputation: 19434
check double/integer type price. (cannot type comma and only type one decimal)
r'^(?:[1-9]\d+|\d)(?:\.\d*)?$';
Upvotes: 0
Reputation: 78971
To match the currency you can use this regex
^(?:[1-9]\d+|\d)(?:\.\d\d)?$
Upvotes: 1
Reputation: 52185
Something like this should work: ^\d+(\.\d{2})?$
This basically states the following:
^\d+
: Start from the beginning of the string (^
) and match one or more digits (\d+
).(\.\d{2})?
: Match a period character (\.
) followed by exactly 2 digits (\d{2}
). The question mark denotes that the value within the brackets can either exist or not, meaning that it will match either 1 or 0 instances of the pattern.$
: The string must end here.Upvotes: 3
Reputation: 146053
Allow 0 or 1 dots, with only digits before and after...
/^\d*\.{0,1}\d*$/
Upvotes: 0
Reputation: 58560
You probably forgot to escape the regex .
operator (which means match any character). Only guessing since you forgot an important piece of information in your question: the failing regexes you have already tried!
Upvotes: 0