Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for decimal number

Tags:

c#

regex

decimal

I need to validate a textbox input and can only allow decimal inputs like: X,XXX (only one digit before decimal sign and a precision of 3).

I'm using C# and try this ^[0-9]+(\.[0-9]{1,2})?$?

like image 228
davorn Avatar asked Jun 09 '09 08:06

davorn


People also ask

What is the regex for decimal number?

\d* - 0 or more digits (the decimal part);

How do you write numbers in regular expressions?

The [0-9] expression is used to find any character between the brackets. The digits inside the brackets can be any numbers or span of numbers from 0 to 9. Tip: Use the [^0-9] expression to find any character that is NOT a digit.

How do I match a number in regex?

To match any number from 0 to 9 we use \d in regex. It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range.


1 Answers

^[0-9]([.,][0-9]{1,3})?$ 

It allows:

0 1 1.2 1.02 1.003 1.030 1,2 1,23 1,234 

BUT NOT:

.1 ,1 12.1 12,1 1. 1, 1.2345 1,2345 
like image 54
J-16 SDiZ Avatar answered Oct 04 '22 01:10

J-16 SDiZ