I need help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rsr2
    New Member
    • Jan 2007
    • 8

    I need help

    Ok I am doing a project at school (I am allowed to get help so don't ask) that Validates Phone Numbers. Basically all i need to do is make so that when you type something in the text box. It accepts dashes. Not Letters. When i do this

    If IsNumeric(txtPh one.Text) = False Then
    MsgBox("Please Put in a valid Phone Number")

    That is what i put. But if you put in a dashes it will say the same and i don't want that. How can i fix this problem?
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by rsr2
    Ok I am doing a project at school (I am allowed to get help so don't ask) that Validates Phone Numbers. Basically all i need to do is make so that when you type something in the text box. It accepts dashes. Not Letters. When i do this

    If IsNumeric(txtPh one.Text) = False Then
    MsgBox("Please Put in a valid Phone Number")

    That is what i put. But if you put in a dashes it will say the same and i don't want that. How can i fix this problem?
    You'll get the error message when dashes are entered because numbers with dashes in the middle of them aren't numbers. You need to check every character in the string to make sure that it is valid phone number text...somethin g like the function I've posted below that checks for numbers, whitespace and "( ) . -"

    Code:
    Private Function ValidNumbers(ByVal Value As String) As Boolean
            'Function Description:
            '   This function is used to validate strings to make it contains valid phone number text.
            '   For a string to be a valid number phone number it must only contain whitespace,  numbers, "."'s, "-"'s, or "()"'s
            'Paramaters:
            '   Value: the string we are checking
            'Retrun Values:
            '   True if the string is valid, otherwise false.
            Dim retval As Boolean
            Dim checkChars() As Char
            retval = True
            Try
                checkChars = Value.ToCharArray
                For Each c As Char In checkChars
                    If Not (Char.IsDigit(c) Or Char.IsWhiteSpace(c) Or c = "." Or c = "-" Or c = ")" Or c = "(") Then
                        retval = False
                    End If
                Next
            Catch ex As Exception
                retval = False
            End Try
            Return retval
        End Function
    Please note that the user can enter any of the valid characters at any point in the phone number. This means the user could enter valid characters but the phone number might be invalid according to North American standards since -9(8.-87.28)221-987 would be considered valid.

    If order matters to you, check out regular expressions.

    Hope this helps
    -Frin

    Comment

    • rsr2
      New Member
      • Jan 2007
      • 8

      #3
      Im new to coding. where in that statement can i put a error messge.....

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by rsr2
        Im new to coding. where in that statement can i put a error messge.....

        Call that function from your main code....if it returns false...display an error message.

        -F

        Comment

        • rsr2
          New Member
          • Jan 2007
          • 8

          #5
          Thank you.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by rsr2
            Thank you.

            Glad to have helped. If you need any more help feel to post again.

            Comment

            Working...