|
|
Title | Validate phone numbers, zip codes, and Canadian postal codes |
Keywords | validate, validation, phone number, Zip code, Canadian postal codes, postal code, Canada |
Categories | Software Engineering |
|
|
Use the LIKE statement to validate the format. The functions used in this program make validating these fields easy.
|
|
' Validate a phone number. If there is an error,
' tell the user and return True.
Public Function ValidatePhoneNumber(ByVal text_box As _
TextBox, ByVal required As Boolean, ByVal _
allow_short_format As Boolean, ByVal allow_long_format _
As Boolean) As Boolean
Dim txt As String
' Make sure some format is allowed.
If Not (allow_short_format Or allow_long_format) Then _
Stop
' Assume the field is valid.
ValidatePhoneNumber = False
' Allow a blank entry if not required.
txt = text_box.Text
If (Not required) And (Len(txt) = 0) Then Exit Function
' Check short format.
If (allow_short_format) And (txt Like "###-####") Then _
Exit Function
' Check long format.
If (allow_long_format) And (txt Like "(###)###-####") _
Then Exit Function
' The validation fails.
' Display an error message.
If allow_short_format And allow_long_format Then
MsgBox "Phone number should have format ###-#### or " & _
"(###)###-####."
ElseIf allow_short_format Then
MsgBox "Phone number should have format ###-####."
ElseIf allow_long_format Then
MsgBox "Phone number should have format " & _
"(###)###-####."
End If
' Set focus to the control.
text_box.SelStart = 0
text_box.SelLength = Len(txt)
text_box.SetFocus
ValidatePhoneNumber = True
End Function
' Validate a Zip code. If there is an error,
' tell the user and return True.
Public Function ValidateZip(ByVal text_box As TextBox, _
ByVal required As Boolean, ByVal allow_short_format As _
Boolean, ByVal allow_long_format As Boolean) As Boolean
Dim txt As String
' Make sure some format is allowed.
If Not (allow_short_format Or allow_long_format) Then _
Stop
' Assume the field is valid.
ValidateZip = False
' Allow a blank entry if not required.
txt = text_box.Text
If (Not required) And (Len(txt) = 0) Then Exit Function
' Check short format.
If (allow_short_format) And (txt Like "#####") Then _
Exit Function
' Check long format.
If (allow_long_format) And (txt Like "#####-####") Then _
Exit Function
' The validation fails.
' Display an error message.
If allow_short_format And allow_long_format Then
MsgBox "Zip code should have format ##### or " & _
"#####-####."
ElseIf allow_short_format Then
MsgBox "Zip code should have format #####."
ElseIf allow_long_format Then
MsgBox "Zip code should have format #####-####."
End If
' Set focus to the control.
text_box.SelStart = 0
text_box.SelLength = Len(txt)
text_box.SetFocus
ValidateZip = True
End Function
' Validate a Canadian Postal Code. If there is
' an error, tell the user and return True.
Public Function ValidateCanadianPostalCode(ByVal text_box _
As TextBox, ByVal required As Boolean) As Boolean
Dim txt As String
' Assume the field is valid.
ValidateCanadianPostalCode = False
' Allow a blank entry if not required.
txt = text_box.Text
If (Not required) And (Len(txt) = 0) Then Exit Function
' Check the format.
If (UCase$(txt) Like "[A-Z]#[A-Z] #[A-Z]#") Then Exit _
Function
' The validation fails.
' Display an error message.
MsgBox "Postal Code should have format A1B 2C3."
' Set focus to the control.
text_box.SelStart = 0
text_box.SelLength = Len(txt)
text_box.SetFocus
ValidateCanadianPostalCode = True
End Function
|
|
|
|
|
|