⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 validationfunctions.vb

📁 Beginning VB.NET DatabasesAll_Code.rar
💻 VB
字号:
Module ValidationFunctions

#Region " Private Variables "
    Private bytPhoneLength As Byte = 10

    Private bytPasswordLength As Byte = 9
    Private blnPasswordUpperCaseCharacters As Boolean = True
    Private blnPasswordNumericCharacters As Boolean = True
    Private strPasswordNumericCharacters As String = "0123456789"
    Private blnPasswordSpecialCharacters As Boolean = True
    Private strPasswordSpecialCharacters As String = "!@#$%^&*()"
#End Region

#Region " Public Variables "
    Public strErrorMessage As String = String.Empty
#End Region

#Region " Public Validation Functions "
    Public Function IsValidPhoneNumber(ByVal strPhoneNumber As String) As Boolean
        'Remove all special characters (e.g. ( ) - . and spaces)
        strPhoneNumber = strPhoneNumber.Replace("(", "").Replace( _
            ")", "").Replace("-", "").Replace(".", "").Replace(Chr(34), "")
        If Not IsNumeric(strPhoneNumber) Then
            'Set the error message and return False
            strErrorMessage = "Phone number cannot contain any alpha characters."
            Return False
        End If
        If strPhoneNumber.Length = bytPhoneLength Then
            'Clear the error message and return True
            strErrorMessage = String.Empty
            Return True
        Else
            'Set the error message and return False
            strErrorMessage = "Phone number must contain " & _
                bytPhoneLength.ToString & " digits."
            Return False
        End If
    End Function

    Public Function IsValidEmail(ByVal strEmail As String) As Boolean
        'Declare local variables
        Dim strDomainName As String
        Dim strDomainType As String

        'Validate an at sign exists
        If strEmail.IndexOf("@") = -1 Then
            'Set the error message and return False
            strErrorMessage = "Email field does not contain a valid " & _
                "email address in the form of username@domainname.domaintype " & _
                "(e.g. testuser@mydomain.com)"
            Return False
        End If

        'Validate a period exists after the at sign
        If strEmail.IndexOf(".", strEmail.IndexOf("@") + 1) = -1 Then
            'Set the error message and return False
            strErrorMessage = "Email field does not contain a valid " & _
                "email address in the form of username@domainname.domaintype " & _
                "(e.g. testuser@mydomain.com)"
            Return False
        End If

        'Validate that the at sign is not the first character
        If strEmail.StartsWith("@") Then
            strErrorMessage = "Email field does not contain a valid " & _
                "email address in the form of username@domainname.domaintype " & _
                "(e.g. testuser@mydomain.com)"
            Return False
        End If

        'Validate that the domain name is at least two characters in length
        strDomainName = strEmail.Substring(strEmail.IndexOf("@") + 1, _
            strEmail.IndexOf(".", strEmail.IndexOf("@") + 1) _
            - strEmail.IndexOf("@") - 1)
        If strDomainName.Length < 2 Then
            'Set the error message and return False
            strErrorMessage = "The domain name in the email address is invalid"
            Return False
        End If

        'Validate that the domain type is at least two characters in length
        strDomainType = strEmail.Substring(strEmail.IndexOf(".", _
            strEmail.IndexOf("@") + 1) + 1)
        If strDomainType.Length < 2 Then
            'Set the error message and return False
            strErrorMessage = "The domain type in the email address is invalid"
            Return False
        End If

        'If we made it this far everything is OK,
        'Clear the error message and return True
        strErrorMessage = String.Empty
        Return True
    End Function

    Public Function IsValidPassword(ByVal strPassword As String) As Boolean
        'Declare local variables
        Dim intIndex As Integer
        Dim blnContainsUpper As Boolean = False
        Dim blnContainsLower As Boolean = False
        Dim blnContainsNumeric As Boolean = False
        Dim blnContainsSpecial As Boolean = False

        'Validate the length
        If strPassword.Length < bytPasswordLength Then
            'Set the error message and return False
            strErrorMessage = "The password does not meet the minimum " & _
                "password length of " & bytPasswordLength.ToString & " characters."
            Return False
        End If

        'Validate lower case alpha characters (always required)
        For intIndex = 0 To strPassword.Length - 1
            If strPassword.Substring(intIndex, 1) Like "[a-z]" Then
                blnContainsLower = True
                Exit For
            End If
        Next
        If Not blnContainsLower Then
            'Set the error message and return False
            strErrorMessage = "The password does not contain any lower " & _
                "case characters."
            Return False
        End If

        'Validate upper case alpha characters
        If blnPasswordUpperCaseCharacters Then
            For intIndex = 0 To strPassword.Length - 1
                If strPassword.Substring(intIndex, 1) Like "[A-Z]" Then
                    blnContainsUpper = True
                    Exit For
                End If
            Next
        End If

        'Validate numeric characters
        If blnPasswordNumericCharacters Then
            If strPassword.IndexOfAny( _
                strPasswordNumericCharacters.ToCharArray) <> -1 Then
                blnContainsNumeric = True
            End If
        End If

        'Validate special characters
        If blnPasswordSpecialCharacters Then
            If strPassword.IndexOfAny( _
                strPasswordSpecialCharacters.ToCharArray) <> -1 Then
                blnContainsSpecial = True
            End If
        End If

        'Final validations
        If blnPasswordUpperCaseCharacters And Not blnContainsUpper Then
            'Set the error message and return False
            strErrorMessage = "The password does not contain any upper " & _
                "case characters."
            Return False
        End If

        If blnPasswordNumericCharacters And Not blnContainsNumeric Then
            'Set the error message and return False
            strErrorMessage = "The password does not contain any " & _
                "numeric characters."
            Return False
        End If

        If blnPasswordSpecialCharacters And Not blnContainsSpecial Then
            'Set the error message and return False
            strErrorMessage = "The password does not contain any " & _
                "special characters."
            Return False
        End If

        'If we made it this far everything is OK,
        'Clear the error message and return True
        strErrorMessage = String.Empty
        Return True
    End Function
#End Region

#Region " Public Formatting Functions "
    Public Function FormatPhoneNumber(ByVal strPhoneNumber As String) As String
        'Remove all special characters (e.g. ( ) - . and spaces)
        strPhoneNumber = strPhoneNumber.Replace("(", "").Replace( _
            ")", "").Replace("-", "").Replace(".", "").Replace(Chr(34), "")

        'Now format the phone number
        FormatPhoneNumber = strPhoneNumber.Substring(0, 3) & "-" & _
            strPhoneNumber.Substring(3, 3) & "-" & _
            strPhoneNumber.Substring(6, 4)
    End Function
#End Region
End Module

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -