Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
Hello everyone,

I need to write a regular expression which will allow numbers like

1.23

and

1

in to the text-box. Decimal point is not the compulsion.

Any suggestion or links are welcome..!!
Posted

Regular Expression is : ^[0-9]\d{0,9}(\.\d{1,3})?%?$

[0-9] : for integers which allows integers from 0 to 9
d{0,9}: this will take up to 10 digits
\. : for decimal
d{1,3} : for decimal up to three digits
 
Share this answer
 
A digit in the range 1-9 followed by zero or more other digits:
C#
^[1-9]\d*$

To allow numbers with an optional decimal point followed by digits. A digit in the range 1-9 followed by zero or more other digits then optionally followed by a decimal point followed by at least 1 digit:
C#
^[1-9]\d*(\.\d+)?$

Simple regular expression for a decimal with a precision of 2[^]
Regular expression to match numbers with or without commas and decimals in text[^]
107 regular expressions[^]
 
Share this answer
 
Comments
vaibhav mahajan 24-Jul-12 2:50am    
thnks codeBegin..!!
thanhtt90 16-Oct-13 5:07am    
Greate solution. Thanks very much :)
Chethan T R 18-Sep-14 9:58am    
In WPF app, i have code like this

private void txtZ_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = regex.IsMatch(e.Text);
}

and my regex is Regex regex = new Regex(@"^[1-9]\d*(\.\d+)?$"); same what you have given. But its not taking dot(.) atleat once

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900