Rookieatthis
Rookieatthis

Reputation: 157

regex to allow decimals, but no commas

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

Answers (5)

BIS Tech
BIS Tech

Reputation: 19434

check double/integer type price. (cannot type comma and only type one decimal)

 r'^(?:[1-9]\d+|\d)(?:\.\d*)?$';

Upvotes: 0

Starx
Starx

Reputation: 78971

To match the currency you can use this regex

^(?:[1-9]\d+|\d)(?:\.\d\d)?$

Upvotes: 1

npinti
npinti

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

DigitalRoss
DigitalRoss

Reputation: 146053

Allow 0 or 1 dots, with only digits before and after...

/^\d*\.{0,1}\d*$/

Upvotes: 0

Kaz
Kaz

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

Related Questions