Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have made this for 1 decimal place but don't know how to limit upto 6

accepted value
6.0
1.1
0.0
2.1
5.9

not accepted
6.1
6.01
6.2
0.00
5.99



"^[0-9]+(\.[0-9]{1,1})?$"


What I have tried:

i have made this for 1 decimal place but don't know how to limit upto 6 ^[0-9]+(\.[0-9]{1,1})?$"
Posted
Updated 28-Apr-17 0:01am
Comments
Tomas Takac 28-Apr-17 4:08am    
Why do you need to validate the value with the regex? It would be easier to parse the value and then check the range.
Richard MacCutchan 28-Apr-17 4:19am    
Why are you allowing 0-9 as the first digit when 6 is supposed to be the limit?

Don't use a regex: Use a basic regex to process the overall format for "digit followed by optional single decimal place":
^(\d(\.\d))$

Then use Decimal.TryParse to convert it to a number, and check the bounds on that.
You can use a regex to check for 0.0 to 6.0, but it's a PITA to maintain if there is a trivial change later. Much easier to understand and modify later if you use code as well.
 
Share this answer
 
Comments
Akhil Jain 28-Apr-17 4:57am    
didn't worked
You have to differentiate between 6[.0] and 0[.d] to 5[.d]:

^(6(\.0)?)|([0-5](\.[0-9])?)$
^((6(\.0)?)|([0-5](\.[0-9])?))$

The above allows numbers without fractional digits. If the fractional digits must be supplied it is simpler:
^(6\.0)|([0-5]\.[0-9])$
^((6\.0)|([0-5]\.[0-9]))$
 
Share this answer
 
v2
Comments
Akhil Jain 28-Apr-17 4:58am    
both are accepting 6.02
Jochen Arndt 28-Apr-17 5:17am    
Uups.
I forgot the surrounding parentheses.
Akhil Jain 28-Apr-17 21:51pm    
not accepting 6.0
Just a few interesting links to help building and debugging RegEx.
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
 
^([0-5]\.\d|6\.0)$


i got it
 
Share this answer
 

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