17
Validation "Programming today is a race between software engineers striving to build bigger and better idiot- proof programs and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.“ Rich Cook

Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

Embed Size (px)

Citation preview

Page 1: Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

Validation"Programming today is a race between

software engineers striving to build bigger and better idiot-proof programs and the

Universe trying to produce bigger and better idiots. So far, the Universe is winning.“

Rich Cook

Page 2: Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

Robust and ResponsiveRobust means that a user may type any data

they like into the program and it won’t crash

Responsive means that the program will tell the user what they have done wrong and what they should do to correct the error

Page 3: Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

What is wrong with the following?

Dim FirstName as IntegerFirstName=”Fred”

Page 4: Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

What is wrong with the following?Dim Age as IntegerAge = txtAge.Text

Page 5: Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

IsNumeric‘declare a variable to store the ageDim Age as Integer

‘test the data typed in the text box to see if it is a numberIf IsNumeric(txtAge.Text)=True Then

‘if it is ok read in the valueAge = txtAge.Text

Else‘if not ok then show an errorlblError.Text = "The age is not valid"

End If

Page 6: Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

IsDate‘declare a variable to store the date of birthDim DOB as Date

‘test the data typed in the text box to see if it is a dateIf IsDate(txtDOB.Text)=True Then

‘if it is ok read in the valueDOB = txtDOB.Text

Else‘if not ok then show an errorlblError.Text = "The date of birth is not a

valid date"End If

Page 7: Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

Testing for a blank field (=“”)‘declare a variable to store first nameDim FirstName as String

‘test the data typed in the text box to see if it isn’t blankIf txtFirstName.Text<>"" Then

‘if it is ok read in the valueFirstName = txtFirstName.Text

Else‘if not ok then show an errorlblError.Text = "You must enter your first

name."End If

Page 8: Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

Testing Number of Characters (Len)‘declare a variable to store first nameDim FirstName as String

‘test the data typed in the text box is not over 20 charactersIf Len(txtFirstName.Text)<=20 Then

‘if it is ok read in the valueFirstName = txtFirstName.Text

Else‘if not ok then show an errorlblError.Text = "First name must not exceed

20 letters."End If

Page 9: Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

Validating an Email Address (InStr)‘declare a variable to store the email addressDim EMail as String

‘test the data typed in the text box has an @ symbolIf InStr(txtEMail.Text, "@") >0 Then

‘if it is ok read in the valueEMail = txtEMail.Text

Else‘if not ok then show an errorlblError.Text = "Not a valid email

address."End If

Question – What is the problem with the above code?

Page 10: Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

The problem is…that the following are all valid email addresses..

fred@@fred@@@@@@@@@@@@

Page 11: Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

An Email Address Must Have… “@” symbol e.g. @hotmail.com

“.” symbol e.g. hotmail.com

At least 5 characters (?)

Page 12: Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

(What is wrong with the following if we use fred@nothing ?) Dim EMail as String

‘test the data typed in the text box has an @ symbolIf InStr(txtEMail.Text, "@") >0 Then

EMail = txtEMail.TextElse

lblError.Text = "Not a valid email address." End If

‘test the address is long enoughIf Len(txtEMail.Text) >5 Then

EMail = txtEMail.TextElse

lblError.Text = "The address is too short." End If

‘test the data typed in the text box has an . symbolIf InStr(txtEMail.Text, ".") >0 Then

EMail = txtEMail.TextElse

lblError.Text = "Not a valid email address." End If

Page 13: Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

Better???

‘declare a variable to store the email address Dim EMail as String

‘test the data typed in the text box has an @ symbolIf InStr(txtEMail.Text, "@") >0 Then

‘test the address is long enoughIf Len(txtEMail.Text) >5 Then

‘test the data typed in the text box has an . symbol

If InStr(txtEMail.Text, ".") >0 Then‘if it is ok read in the valueEMail = txtEMail.Text

ElselblError.Text = "Not a valid email

address." End If

ElselblError.Text = "The address is too short."

End IfElse

lblError.Text = "Not a valid email address no @ symbol."

End If

Page 14: Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

Or….

‘declare a variable to store the email address Dim EMail as String

‘clear the error message labellblError.Text = ""‘test the data typed in the text box has an @ symbolIf InStr(txtEMail.Text, "@") = 0 Then

lblError.Text = "No @ symbol." End If

‘test the address is long enoughIf Len(txtEMail.Text) <=5 Then

lblError.Text = lblError.Text & " The address is too short."

End If‘test the data typed in the text box has a . symbolIf InStr(txtEMail.Text, ".") = 0 Then

lblError.Text = lblError.Text & "No dot." End If If lblError.Text = "" Then Email = txtEmail.Text End IF

Page 15: Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

Validation in ActionWe shall look at the validation for the save button on

“your details”

Page 16: Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

Questions1. What is wrong with the following validation? Dim Age as IntegerAge = txtAge.TextIf IsNumeric(Age) = True Then

If Age > 18 ThenlblMessage.Text = “You are old enough”

End IfEnd If

Page 17: Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to

Questions2. Write code that validates if the text in txtName is

less than 40 characters. If the length of the name is OK then a message should appear welcoming the person.

 

3. Write code that validates the text in txtPostCode to ensure that it is not blank.

 

4. Write code that validates the values in the fields, txtStartAge and txtEndAge also making sure that the start age is not greater than or equal to the end age.