none
Validating input RRS feed

  • Question

  • Hi everyone, I'm working on a console application that asks for a customer's age. It needs to validate that the age is within 0 and 120 years and also that the input is only a number between 0 and 120 i.e. no letters should be allowed.  I've been able to get the program to validate only ages within 0 and 120 however I can't get the program to display an error code if a letter is entered rather than a number.  I tried to accomplish this with a while repetition in the upper half of the block.  Any help is appreciated.  Here is my code:

    P.S. this is a Lab for a class, we haven't covered RegEx yet so I don't think my instructor would want me using this expression.

    Console.Write("Please enter the customer's age: "); //Prompts user to input the customer's age
            newCustomer.CustomerAge = Console.ReadLine(); //Reads user's input and sets CustomerAge property
            Console.WriteLine(); //Outputs a blank line

            while (newCustomer.CustomerAge.Equals("")) //While repetition used to validate the age entered isn't a blank string
            {
                Console.WriteLine("Your input of {0} is invalid.  Please enter an age between 0 and 120 years", Convert.ToString(newCustomer.CustomerAge));
                newCustomer.CustomerAge = Console.ReadLine();
                Console.WriteLine();

                if (Convert.ToInt32(newCustomer.CustomerAge) < 0 || Convert.ToInt32(newCustomer.CustomerAge) > 120) //Validates customer's age to confirm it is within 0 and 120 years
                {
                    Console.WriteLine("Your input of {0} is invalid.  Please enter an age between 0 and 120 years", Convert.ToString(newCustomer.CustomerAge));
                    newCustomer.CustomerAge = Console.ReadLine();
                    Console.WriteLine();
                }
            }

    Saturday, March 1, 2014 8:57 PM

Answers

  • The following sample code should give you the picture of how to use the int.TryParse method to determine if a string contains a valid integer value:           

    do
                {
                    Console.Write("Please enter the customer's age: "); //Prompts user to input the customer's age
                    newCustomer.CustomerAge = Console.ReadLine();
    
                    int age;
                    if (!int.TryParse(newCustomer.CustomerAge, out age))
                    {
                        //invalid number...
                        Console.WriteLine("Your input of {0} is invalid.  Please enter an age between 0 and 120 years", Convert.ToString(newCustomer.CustomerAge));
                    }
                    else if (age < 0 || age > 120)
                    {
                        //invalid age...
                        Console.WriteLine("Your input of {0} is invalid.  Please enter an age between 0 and 120 years", Convert.ToString(newCustomer.CustomerAge));
                    }
                    else
                    {
                        Console.WriteLine("Valid!");
                    }
    
                } while (true);
    

    Sunday, March 2, 2014 8:41 PM

All replies

  • Have you already covered the following ways:

    • Using int.TryParse to validate a number,
    • Or calling Convert.ToInt32 inside a try block and intercept exceptions inside a catch block?
    • Proposed as answer by Pantelis44999 Monday, March 3, 2014 12:41 AM
    Sunday, March 2, 2014 7:12 PM
  • The following sample code should give you the picture of how to use the int.TryParse method to determine if a string contains a valid integer value:           

    do
                {
                    Console.Write("Please enter the customer's age: "); //Prompts user to input the customer's age
                    newCustomer.CustomerAge = Console.ReadLine();
    
                    int age;
                    if (!int.TryParse(newCustomer.CustomerAge, out age))
                    {
                        //invalid number...
                        Console.WriteLine("Your input of {0} is invalid.  Please enter an age between 0 and 120 years", Convert.ToString(newCustomer.CustomerAge));
                    }
                    else if (age < 0 || age > 120)
                    {
                        //invalid age...
                        Console.WriteLine("Your input of {0} is invalid.  Please enter an age between 0 and 120 years", Convert.ToString(newCustomer.CustomerAge));
                    }
                    else
                    {
                        Console.WriteLine("Valid!");
                    }
    
                } while (true);
    

    Sunday, March 2, 2014 8:41 PM