Click here to Skip to main content
15,916,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to use regular expression for entering integer values in between 18 to 80. Help me to find a proper solution. Thank you.
Posted

Don't.
Regexes are not good at "value" comparisons. You pretty much have to do something like
^(18|19|2\d|3\d|4\d|5\d|6\d|7\d|80)$
which will work, but...it's pretty nasty.

This kind of validation should be done in code by parsing the user input to a number then checking it's range, not via a regular expression.
 
Share this answer
 
Comments
Maciej Los 23-Dec-14 6:53am    
+5
For range of numbers: 10, 12, 18, 52, 79, 85
pattern: 1[8-9]|[2-7][0-9]|8[0]
matches: 18, 52, 79

More: http://www.regular-expressions.info/numericranges.html[^]

[EDIT]
OriginalGriff is right. Regex may be useful if you would like to find numeric value (from range) in text.

[EDIT 2]
Simple if should be enough:
C#
if(enterednumber>=17 && enterednumber<=80)
{
//true part
}
else
{
//false part
}

or via lambda expressions:
C#
int vfrom = 18;
int vto = 80;
int number = 17;
bool result = number >= vfrom && number <= vto ? true : false;

Console.WriteLine("Does {0} is in between {1} and {2}? Result: {3}", number, vfrom, vto, result);

Result: Does 17 is in between 18 and 80? Result: False
 
Share this answer
 
v4

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