Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone help me with the regular expression for any number with maximum of 1 decimal point

C#
Valid numbers: 
10
10.2
100015.1
99.9
10.0

Invalid numbers:
10.01
12.333
0.233
10.
10.1<space>
10.<space>


What I have tried:

i tried with below expression but it does not work for (10. , 10.space , 10.1space)

\.\d\d+
Posted
Updated 25-Aug-16 0:02am
v3
Comments
Member 13908363 10-Mar-20 3:06am    
how to restrict more than one decimal points ? the user should not write like 5..23
but he can write 5.23

Try:
^\d\d+\.[1-9]\d*$

That allows two or more to the left of the decimal point, and at least one on the right.
And allows for 10.01 being invalid.

"I am searching for that allows two or more to the left of the decimal point, and max one on the right."
Then try:
^\d\d+(\.[1-9])?$
 
Share this answer
 
v2
Comments
Shrikesh_kale 25-Aug-16 5:43am    
I am searching for that allows two or more to the left of the decimal point, and max one on the right.
OriginalGriff 25-Aug-16 5:50am    
Answer updated
Shrikesh_kale 25-Aug-16 6:21am    
thanks
Member 13908363 10-Mar-20 3:06am    
how to restrict more than one decimal points ? the user should not write like 5..23
but he can write 5.23
OriginalGriff 10-Mar-20 3:13am    
Did you try the regex that has been here for three and a half years?
Try to replace \.\d\d+ with \d+\.\d as a start.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 
v2
Comments
Member 13908363 10-Mar-20 3:06am    
how to restrict more than one decimal points ? the user should not write like 5..23
but he can write 5.23
Patrice T 10-Mar-20 3:24am    
Ypu should ask your own question.
add some details .

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