Small Basic: Three Ways to Validate Input


Validate Your User Input

When you create a program that reads data from your user, always check the input data right away! We call this process validation, and it’s a regular programming practice.

Here are three ways you can handle invalid input values:

1)     Display an error message and end the application.

2)     Override the invalid value with a default “good” value and continue with the program.

3)     Continue to prompt your user until she enters a valid input.

The best approach depends on the problem. You can also stand behind your user and watch her while she’s typing to make sure it’s 100% correct, but that would be creepy and hard to scale across all your users, so we’ll discuss some other options instead.

You need your user to enter a number between 1 and 5 (inclusive). If she enters a number that’s less than 1 or greater than 5, you’ll prompt her to enter it again, until she gets it right. One way to check if she entered the right number is to use a Goto statement.

TryAgain:

TextWindow.Write(“Enter a number between 1 and 5: “)

num = TextWindow.ReadNumber()

If ((num < 1) Or (num > 5)) Then

  Goto TryAgain

EndIf

  

The code checks the input data and, if it’s invalid, it prompts your user again. It works okay if you only have a couple inputs. But if you need to validate many inputs, then using multiple Goto statements can make your program unreadable and turn it into a tangled mess (like Rapunzel’s bad hair day). See Small Basic: Spaghetti Code.

Instead, you can use a While loop, like this:

1 TextWindow.Write(“Enter a number between 1 and 5: “)

2 num = TextWindow.ReadNumber()

3 While ((num < 1) Or (num > 5)) ‘ As long as the data’s invalid

4   TextWindow.Write(“Enter a number between 1 and 5: “)

5   num = TextWindow.ReadNumber()

6 EndWhile

 

After you prompt your user for data and read her input number (lines 1-2), the While loop checks if the input number’s valid (line 3). If num is outside your range, the loop runs its body (lines 4-5) and asks your user to reenter the data. This loop repeats as long as she enters an invalid number.

Although this code works fine, it repeats the statements to prompt and read data (lines 1-2) inside the loop’s body (lines 4-5).

 

You can remove the first two statements by forcing your way into the loop’s body, as shown here:

1 ‘ Using While to to validate an input number

2 num = -1   ‘Invalid value (to force a pass through the loop)

3

4 While ((num < 1) Or (num > 5))

5   TextWindow.Write(“Enter a number between 1 and 5: “)

6   num = TextWindow.ReadNumber()

7 EndWhile

8 TextWindow.WriteLine(“You entered: ” + num)

In line 2, your program sets the variable num to –1 so that the While loop (line 4) is true and runs at least once. Your program prompts your user to enter a number and passes it to the num variable (lines 5-6). The loop’s checked again. If num is below 1 or above 5 (which means she entered an invalid number), the loop’s body runs again, and she’s asked to enter another number. If num is between 1 and 5, the loop ends, and your program moves to line 8 to display the number she entered.

 

Here’s an example of your user not paying much attention to your instructions, so the question repeats until she enters an acceptable number:

Enter a number between 1 and 5: 0

Enter a number between 1 and 5: 6

Enter a number between 1 and 5: 3

You entered: 3

We showed you three ways to prompt your user until she enters the right answer. Pick the way you feel most comfortable with, but you should know the other ones because you’ll see them when you read other people’s code.

 

Do you have any questions? Ask us! We’re full of answers and other fine things!

Head to the Small Basic forum to get the most answers to your questions: 

http://social.msdn.microsoft.com/Forums/en-US/smallbasic/threads/   

And go to http://blogs.msdn.com/SmallBasic to download Small Basic and learn all about it!

Small and Basically yours,

   – Ninja Ed & Majed Marji

Comments (5)

  1. سئو سایت says:

    Thanks .

    <a href="http://www.newseo.ir">سئو سایت</a>

  2. You're welcome! Thanks for the HTML code!

  3. Dominik says:

    Is there a way to validate, whether the user entered a number?
    For example: I wrote a program to solve quadratic equations, where the user has to enter the values of a,b and c, when the equation looks like this: a*x²+b*x+c=0. If numbers are entered, there is no problem what so ever, but if no number is entered or if characters other than numbers are entered, the program will crash.
    Thus, again: can I validate in some way, whether a number was entered?

    Thanks in advance.

    1. Dominik, this will require a conversation. Please ask this question to engage in the forum:

      https://social.msdn.microsoft.com/Forums/en-US/home?forum=smallbasic

      Thanks!

Skip to main content