1 / 17

Validation

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. Robust and Responsive.

haines
Download Presentation

Validation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Robust and Responsive • Robust 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

  3. What is wrong with the following? Dim FirstName as Integer FirstName=”Fred”

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

  5. IsNumeric ‘declare a variable to store the age Dim Age as Integer ‘test the data typed in the text box to see if it is a number If IsNumeric(txtAge.Text)=True Then ‘if it is ok read in the value Age = txtAge.Text Else ‘if not ok then show an error lblError.Text = "The age is not valid" End If

  6. IsDate ‘declare a variable to store the date of birth Dim DOB as Date ‘test the data typed in the text box to see if it is a date If IsDate(txtDOB.Text)=True Then ‘if it is ok read in the value DOB = txtDOB.Text Else ‘if not ok then show an error lblError.Text = "The date of birth is not a valid date" End If

  7. Testing for a blank field (=“”) ‘declare a variable to store first name Dim FirstName as String ‘test the data typed in the text box to see if it isn’t blank If txtFirstName.Text<>"" Then ‘if it is ok read in the value FirstName = txtFirstName.Text Else ‘if not ok then show an error lblError.Text = "You must enter your first name." End If

  8. Testing Number of Characters (Len) ‘declare a variable to store first name Dim FirstName as String ‘test the data typed in the text box is not over 20 characters If Len(txtFirstName.Text)<=20 Then ‘if it is ok read in the value FirstName = txtFirstName.Text Else ‘if not ok then show an error lblError.Text = "First name must not exceed 20 letters." End If

  9. Validating an Email Address (InStr) ‘declare a variable to store the email address Dim EMail as String ‘test the data typed in the text box has an @ symbol If InStr(txtEMail.Text, "@") >0 Then ‘if it is ok read in the value EMail = txtEMail.Text Else ‘if not ok then show an error lblError.Text = "Not a valid email address." End If Question – What is the problem with the above code?

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

  11. An Email Address Must Have… • “@” symbol e.g. @hotmail.com • “.” symbol e.g. hotmail.com • At least 5 characters (?)

  12. (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 @ symbol If InStr(txtEMail.Text, "@") >0 Then EMail = txtEMail.Text Else lblError.Text = "Not a valid email address." End If ‘test the address is long enough If Len(txtEMail.Text) >5 Then EMail = txtEMail.Text Else lblError.Text = "The address is too short." End If ‘test the data typed in the text box has an . symbol If InStr(txtEMail.Text, ".") >0 Then EMail = txtEMail.Text Else lblError.Text = "Not a valid email address." End If

  13. Better??? ‘declare a variable to store the email address Dim EMail as String ‘test the data typed in the text box has an @ symbol If InStr(txtEMail.Text, "@") >0 Then ‘test the address is long enough If 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 value EMail = txtEMail.Text Else lblError.Text = "Not a valid email address." End If Else lblError.Text = "The address is too short." End If Else lblError.Text = "Not a valid email address no @ symbol." End If

  14. Or…. ‘declare a variable to store the email address Dim EMail as String ‘clear the error message label lblError.Text = "" ‘test the data typed in the text box has an @ symbol If InStr(txtEMail.Text, "@") = 0 Then lblError.Text = "No @ symbol." End If ‘test the address is long enough If 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 . symbol If InStr(txtEMail.Text, ".") = 0 Then lblError.Text = lblError.Text & "No dot." End If If lblError.Text = "" Then Email = txtEmail.Text End IF

  15. Validation in Action • We shall look at the validation for the save button on “your details”

  16. Questions 1. What is wrong with the following validation? Dim Age as Integer Age = txtAge.Text If IsNumeric(Age) = True Then If Age > 18 Then lblMessage.Text = “You are old enough” End If End If

  17. Questions 2. 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.

More Related