Preview

08 - Validation concepts

 1. Some sites require you to have a password that is at least eight characters in length. This is called a 'range check' and is a type of validation.

  True

  False

 2. What is Validation?

  Validation is the way that coders speak to each other to feel valid

  Validation is looking for valid or appropriate variable names

  Validation is ensuring that the data input into the program is VALID

  Validation is a state of the soul - peace - that coders feel

 3. What is a good example of validation?

  Validating the name field to stop it from accepting integers (assuming people aren't named 23423 etc!)

  Validating the password to field to only accept passwords that are at least six characters long

  All of these options are valid examples of validation!

  Validating the gender field to only accept values of "M" or "F"

 4. One way of validating is simply by ensuring that you specify the data type for your input. In the example below ...
age=int(input("Enter Age:"))

  We are setting the input age to integer (to prevent entry of strings)

  We are setting the input age to integrate with the rest of the variables

  We are setting the input age to cancel itself out so it doesn't affect any thing else in the code

  We are setting the input age to input itself without any valid truncation

 5. Another way of validating input is to use the "try except" block as follows: Type this out yourself and record what happens if you enter the number '3f'
try:
    value=int(input("Type a number:"))
except ValueError:
    print("This is not a whole number.")

  It will say: ValueError

  It will say: Type a number

  It will say: Thank you

  It will say: This is not a whole number

 6. Another advanced method of including validation into a program is by using regular expressions. Feel free to look these up. Type in the program below yourself (or copy and paste) and find out what would happen if you entered password: open123
import re
def main():
          password= input("Create a password : ")
          x = True #random variable x which is set to TRUE
          while x:  #while x is TRUE,then run the following which is in the loop
		# if password is not between 6 and 12 exit the loop	
              if (len(password)<6 or len(password)>12): 
                  print("Password length between 6 and 12 please")     
                  break #exit the WHILE LOOP
              elif not re.search("[a-z]",password): #
                  print("You need at least one lower case letter")
                  break
              elif not re.search("[0-9]",password): 
                  print("You need at least one number")
                  break
              elif not re.search("[A-Z]",password): 
                  print("You need at least one upper case character")
                  break
              elif not re.search("[$#@]",password): 
                  print("You need at least one special character please")
                  break
              elif re.search("\s",password): #if contains blank space
                  print("You cannot have blank spaces in your password...")
                  break
              else:
                  print("Valid Password")
                  x=False 
                  break

          if x: #note this is outside the while loop 
#so the code jumps to it, if BREAK is typed inside the loop
              print("Not a Valid Password")
              main()

main()

  Valid Password

  You need at least one blank space Not a Valid Password

  You need at least one number Not a Valid Password

  You need at least one upper case character Not a Valid Password

 7. Another simple method of coding validation is to use a LIST to store the accepted inputs. Code the example below and then answer the question. If we wanted the input 'p' as well as 'P' to be accepted what would you do?
def play():
  accepted_choices=["P"]
  choice=input("Press P to play:")
  if choice in accepted_choices:
    print("LET'S PLAY")
  else: 
    print("Sorry, please make a valid choice")
  
play()

  Change line 2 to accepted_choices=we accept it all, p, P or whatever ....

  Delete line 2

  Change line 2 to: accepted_choices=["P" or "p"]

  Change line 2 to: accepted_choices=["p","P"]

 8. A good programmer will not only build in validation(to prevent invalid and silly input) but may also add validation messages which ...

  will tell the user they are useless and refuse to let them input data again

  will invalidate the program by making it crash

  will scare the life out of the user by doing something outrageous

  will warn the user when the input is invalid and give them helpful suggestions

 9. Validation is an automatic computer check to ensure that the data entered is sensible and reasonable (i.e valid). It does not check the accuracy of data.

  True

  False

 10. Types of validation include: Presence check (for example to see that the password field is not left blank) and Length check (to check that the input is not too short or too long)

  False

  True