Examples for input validation
Visual Studio 2010 (Similar to VB5, VB6 and Subsequent Visual Basic.NET languages)
For counter = 0 To 4score(counter) = InputBox("Please enter a score between 0 and 50", "Score Entry")While score(counter) ˂ 0 Or score(counter) ˃ 50score(counter) = InputBox("You have not entered a score between 0 and 50. Please try again.", "Score Validation")End WhileNextLiveCode
Remember - Live code will index from 1 rather than 0!
repeat with loop = 1 to 5ask "Please enter your score between 0 and 50"put it into score[loop]repeat while score ˂ 0 or score ˃ 50ask "You have not entered a score between 0 and 50. Please try again."put it into score [loop]end repeatend repeatPython – Version 3.x
for counter in range (5):score[counter]= int(input ('Please enter a score between 0 and 50'))while not (score[counter]˃=0 and score[counter]˂=50):score [counter] = int(input ('You have not entered a score between 0 and 50. Please try again.'))Java
for(int counter=0;counter˂5;counter++){input=JOptionPane.showInputDialog("Please enter a score between 0 and 50");score[counter]=Integer.parseInt(input);while((score[counter]˂0)||(score[counter]˃50)){input=JOptionPane.showInputDialog("You have not entered a score between 0 and 50. Please try again ");score [counter]=Integer.parseInt(input);}}True BASIC
Remember - True BASIC will index from 1 rather than 0!
FOR counter = 1 To 5INPUT PROMPT “Please enter a score between 0 and 50”: resultDO WHILE result ˂ 0 OR result ˃ 50INPUT PROMPT “You have not entered a score between 0 and 50. Please try again.”: resultLOOPLET score(counter) = resultNEXT counterXojo (Formerly REALbasic)
This example presumes that you have access to a pre-designed method for creating an InputBox.
For counter = 0 to 4score(counter)=val(InputBox("Please enter a score between 0 and 50"))While score(counter)˂0 OR score(counter)˃50score(counter)=val(InputBox("Please re-enter your score. Your score must be between 0 and 50"))WendNextPascal
beginFor counter := 0 to 4 dobeginWriteln (‘Please enter a score between 0 and 50’)Readln(result);While result ˂ 0 or result ˃ 50 doBeginWriteln (‘Please re-enter your score. Your score must be between 0 and 50’)Readln(result);End;score[counter] := resultEnd;End.Comal
FOR counter%:= 0 TO 4 DOPRINT "Please enter a score between 0 and 50";INPUT score%(counter%)WHILE (score% ˂ 1) OR (score% ˃ 10) DOPRINT “Please re-enter your score. Your score must be between 0 and 50";INPUT score%(counter%)ENDWHILENEXT