📄 validationfunctions.vb
字号:
Imports System.Text.RegularExpressions
Module ValidationFunctions
'Private variables
Private bytMinimumPasswordLength As Byte = 6
Private bytMaximumPasswordLength As Byte = 9
Private strPasswordSpecialCharacters As String = "!@#$%^&*()"
'Public variables
Public strErrorMessage As String = String.Empty
Public Function IsValidPhoneNumber(ByVal strPhoneNumber As String) As Boolean
'Rules: phone number must be in one of the following formats
' 123 456-7890
' (123) 456-7890
' 123-456-7890
IsValidPhoneNumber = Regex.IsMatch(strPhoneNumber, ( _
"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"))
If IsValidPhoneNumber Then
'Clear the error message
strErrorMessage = String.Empty
Else
'Set the new error message
strErrorMessage = "The phone number entered is invalid." & _
ControlChars.CrLf & ControlChars.CrLf & _
"Phone numbers must be in one of the following formats:" & _
ControlChars.CrLf & _
" 123 456-7890" & ControlChars.CrLf & _
" (123) 456-7890" & ControlChars.CrLf & _
" 123-456-7890"""
End If
End Function
Public Function IsValidEmail(ByVal strEmail As String) As Boolean
'Pattern: name@domain.type
'Rules: name - any character, digit, or special character;
' any number of characters
' domain - any alpha character or digit;
' special characters of - or _ only;
' must contain at least one character or digit
' after a - or _
' type - any domain type (alpha characters only)
' between 2 and 4 characters in length
IsValidEmail = Regex.IsMatch(strEmail, _
("\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-|_)" & _
"[A-Za-z0-9]+)*\.([A-Za-z]{2,4})$"))
If IsValidEmail Then
'Clear the error message
strErrorMessage = String.Empty
Else
strErrorMessage = "The email entered is invalid." & _
ControlChars.CrLf & ControlChars.CrLf & _
"Email addresses must be in the format of: someone@domain.type"
End If
End Function
Public Function IsValidPassword(ByVal strPassword As String) As Boolean
'Rules: must contain at least one upper case character
' must contain at least one lower case character
' must contain at least one numeric digit
' must contain at least one special character
' (defined in strPasswordSpecialCharacters variable)
' must be between the minimum and maximum length
' (defined in the bytMinimumPasswordLength and
' bytMaximumPasswordLength variables)
IsValidPassword = Regex.IsMatch(strPassword, _
("^(?=.*\d)(?=.*[A-Z])(?=.*[a-z])" & _
"(?=.*[" & strPasswordSpecialCharacters & "]).{" & _
bytMinimumPasswordLength & "," & _
bytMaximumPasswordLength & "}$"))
If IsValidPassword Then
'Clear the error message
strErrorMessage = String.Empty
Else
strErrorMessage = "The password entered is not strongly typed." & _
ControlChars.CrLf & ControlChars.CrLf & _
"Passwords must be between " & _
bytMinimumPasswordLength.ToString & _
" and " & bytMaximumPasswordLength.ToString & _
" characters in length and" & ControlChars.CrLf & _
"contain at least one upper case letter, " & _
"one lower case letter," & ControlChars.CrLf & _
"one numeric digit, and one special character." & _
ControlChars.CrLf & _
"Valid special characters are: " & strPasswordSpecialCharacters
End If
End Function
Public Function FormatPhoneNumber(ByVal strPhoneNumber As String) As String
'Remove all special characters (e.g. ( ) - and spaces)
strPhoneNumber = strPhoneNumber.Replace("(", "").Replace( _
")", "").Replace("-", "").Replace(Chr(34), "")
'Now format the phone number for storage in the database
FormatPhoneNumber = strPhoneNumber.Substring(0, 3) & "-" & _
strPhoneNumber.Substring(3, 3) & "-" & _
strPhoneNumber.Substring(6, 4)
End Function
End Module
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -