Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
In my window application i have many text box and i want to enter a number from particular range of values like 1, 2, 4 ,8, 11, 56, 78. i am able to validate single digit numbers, but unable to do with two digit number. how can i validate from only those numbers. please can you help me
Posted
Updated 22-Jan-14 1:58am
v2
Comments
Dinesh.V.Kumar 22-Jan-14 7:28am    
Is this range of numbers finalized?
If yes then have the numbers in a list [arrays, arraylist, or generic list]. when the user enters a value in the textbox, check the value is available in this list. If the value is available then allow the user to proceed else show an error message.

Hope this helps

Regards
Dinesh Kumar.V.
Karthik_Mahalingam 22-Jan-14 7:31am    
post your code..

Try this...
Range Validation from 0 to 78..

C#
public static bool IsValidNo(string inputNo)
{
    string strRegex = @"^(?:\d|[1-3]\d|7[0-8])$";
    Regex re = new Regex(strRegex);
    if (re.IsMatch(inputEmail))
        return (true);
    else
       return (false);
}


For more refer this link
 
Share this answer
 
v4
Try this

If you want to validate only fixed values this will be enough
C#
private static bool ValidateFixedNumbers(int number)
   {
       int[] numbers = { 1, 2, 4, 8, 11, 56, 78 };
       return numbers.Contains(number);
   }

with in a range , you can use this.
C#
private static bool ValidateInRange(int from, int to, int number)
{
    return System.Linq.Enumerable.Range(from, to).Contains(number);

}
 
Share this answer
 
Comments
Satya52 22-Jan-14 8:32am    
it's working. thank you
Karthik_Mahalingam 22-Jan-14 8:53am    
welcome satya :)
Try following code on textBox keypress event

C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
       {
           //textBox1 allows only range from 0 to 50  ****************
           try
           {
               double dVal = Convert.ToDouble(textBox1.Text.Trim().Insert(textBox1.SelectionStart, ((char)e.KeyChar).ToString()));
               if (dVal < 0 || dVal > 50)
               {
                   e.Handled = true;
                   return;
               }
           }
           catch
           {
               //skip
               return;
           }
           //************************************
       }
 
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