Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

ASSIGNMENT 1: DATA VALIDATION IN VB.

NET WINDOWS FORMS – 5 MARKS


GICHERU DUNCAN -21/05493
Comments and Documentation:

Provide comments in your code to explain the purpose of various sections.

Public Class RegistrationForm

' Form load event to set initial properties

Private Sub RegistrationForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

' Set the form title

Me.Text = "Registration Form"

End Sub

' Click event for the Submit button

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

' Call the validation function and if all data is valid, show success message

If ValidateForm() Then

MessageBox.Show("Registration successful!", "Success", MessageBoxButtons.OK,


MessageBoxIcon.Information)

Application.Exit()

End If

End Sub

' Function to validate the form fields

Private Function ValidateForm() As Boolean

' Validate First Name

If String.IsNullOrWhiteSpace(txtFirstName.Text) OrElse Not IsAlphabetic(txtFirstName.Text) Then

MessageBox.Show("First Name must not be empty and should contain only letters.", "Validation
Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

txtFirstName.Focus()

Return False

End If
' Validate Last Name

If String.IsNullOrWhiteSpace(txtLastName.Text) OrElse Not IsAlphabetic(txtLastName.Text) Then

MessageBox.Show("Last Name must not be empty and should contain only letters.", "Validation
Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

txtLastName.Focus()

Return False

End If

' Validate Email Address

If Not IsValidEmail(txtEmail.Text) Then

MessageBox.Show("Please enter a valid email address.", "Validation Error",


MessageBoxButtons.OK, MessageBoxIcon.Error)

txtEmail.Focus()

Return False

End If

' Validate Age

Dim age As Integer

If Not Integer.TryParse(txtAge.Text, age) OrElse age <= 0 OrElse age >= 140 Then

MessageBox.Show("Age must be a positive integer between 1 and 139.", "Validation Error",


MessageBoxButtons.OK, MessageBoxIcon.Error)

txtAge.Focus()

Return False

End If

' All validations passed

Return True

End Function

' Function to check if the string contains only letters


Private Function IsAlphabetic(input As String) As Boolean

For Each c As Char In input

If Not Char.IsLetter(c) Then

Return False

End If

Next

Return True

End Function

' Function to check if the email address is in a valid format

Private Function IsValidEmail(email As String) As Boolean

Try

Dim mail = New System.Net.Mail.MailAddress(email)

Return True

Catch

Return False

End Try

End Function

End Class

Include a brief explanation of your program's functionality and how you approached data validation.

• User Interface:

• The form includes text boxes (TextBox) for input fields: First Name, Last Name, Email
Address, and Age.
• A submit button (Button) is included to trigger the validation process.

• Data Validation:

• First Name and Last Name: The input is checked to ensure it is not empty and contains
only letters. This is done using the IsAlphabetic function which iterates through each
character to ensure it is a letter.
• Email Address: The input is validated using the IsValidEmail function which tries to
create a MailAddress object. If it throws an exception, the email is invalid.
• Age: The input is checked to be a positive integer between 1 and 139 using
Integer.TryParse and conditional checks.

• Error Handling:

• If any field fails validation, an appropriate error message is displayed using


MessageBox.Show and the focus is set to the invalid field.

• Submission:

• If all fields pass validation, a success message is shown and the application exits.
INSTRUCTIONS FOR SUBMITTING YOUR WORK:

Provide your work in either a Word or PDF document in the following format:

Screenshots of the user interface, showing the inputs, error handling and feedback to the user, and
successful submission message

Screenshot of your Code segment showing implementation of data validation for each input
field, organization of code and comments
GRADING RUBRIC:

1. Correct implementation of data validation for each input field. [1Mark]


2. Effective error handling and feedback to the user. [1Mark]
3. User-friendly design and clear instructions. [1Mark]
4. Successful submission message. [1Mark]
5. Clarity and organization of code and comments. [1Mark]

You might also like