Using Visual Basic, with strcit type option on, Create a Function procedure name
ID: 3702352 • Letter: U
Question
Using Visual Basic, with strcit type option on,
Create a Function procedure named IsValidZipCode. The procedure should accept one argument containing the string to validate, and return a Boolean value. A valid zip code contains either five characters or 10 characters. The 10-character, zip+four code has the format xxxxx-xxxx, where x is a digit and the dash is a literal character. The function should return True if the zip code is valid and False otherwise. Create a Button's click event handler, as necessary, to test the procedure.
Explanation / Answer
Hi,
Please create textbox and a button. Double click on the button and paste the code written in the button sub procedure inside that and then paste the function code to check the zip code validations-
Private Sub CommandButton1_Click()
Dim val As Boolean
val = IsValidZipCode(TextBox1.Text)
MsgBox (val)
End Sub
Public Function IsValidZipCode(code As String) As Boolean
If Len(code) = 5 And IsNumeric(code) = True Then
IsValidZipCode = True
ElseIf Len(code) = 10 And InStr(code, "-") = 6 And Len(Mid(code, InStr(code, "-"), Len(code))) - 1 = 4 And IsNumeric(Mid(code, InStr(code, "-"), Len(code))) = True And IsNumeric(Mid(code, 1, InStr(code, "-") - 1)) = True Then
IsValidZipCode = True
End If
End Function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.