IsNumeric()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mwcapps
    New Member
    • Mar 2009
    • 16

    IsNumeric()

    So I'm passing a masked telephone number in the form of (999)999-9999 to a Function that 'strips' the numbers out and puts them into a string that I then use in an Update SQL statement. So far I have the code below that of course doesn't do anything:

    Function StripPhone(ByVa l sPhone as String) as String
    Dim i as Integer
    Dim sStripped as String

    For i = 1 to Len(sPhone)
    If IsNumeric(Mid(s Phone, i)) Then
    sStripped = i
    sStripped = sStripped & Mid(sPhone, i)
    End if
    i = i + 1
    Next
    StripPhone = sStripped
    End Function
  • MrMancunian
    Recognized Expert Contributor
    • Jul 2008
    • 569

    #2
    So what's your question here?

    Steven

    Comment

    • mwcapps
      New Member
      • Mar 2009
      • 16

      #3
      My question is: How do I test the data to see if the characters are numbers? And how do I add them to a string if they are?

      Comment

      • MrMancunian
        Recognized Expert Contributor
        • Jul 2008
        • 569

        #4
        You were pretty close. This is the correct code:

        Code:
        Function StripPhone(ByVal sPhone As String) As String
          Dim i As Integer
          Dim sStripped As String = ""
          For i = 1 To Len(sPhone)
            If IsNumeric(Mid(sPhone, i, 1)) Then
              sStripped = sStripped & Mid(sPhone, i, 1)
            End If
          Next
          Return sStripped
        End Function
        The For-Next-loop already increases i, so you didn't have to do that. sStripped = i doesn't make sense, as you don't want it to be the counter, but a part of the string. Further, Mid is used as Mid(string, start, length). This way, it is crystal clear which part of the string you want to check.

        Steven

        Comment

        • mwcapps
          New Member
          • Mar 2009
          • 16

          #5
          Mr. Steve,

          Thank you sooo much. That worked, which I'm sure you're not surprised. You solved in an hour what I've been working on for 3 weeks. Thanks again for your help.

          Comment

          • aryanbs
            New Member
            • Mar 2009
            • 42

            #6
            Originally posted by mwcapps
            Mr. Steve,

            Thank you sooo much. That worked, which I'm sure you're not surprised. You solved in an hour what I've been working on for 3 weeks. Thanks again for your help.
            I am surprised how do you know it took him an hour?

            Comment

            • aryanbs
              New Member
              • Mar 2009
              • 42

              #7
              And One more thing, If its a MaskedTextBox then there is a property called
              TextMaskFormat, setting it to ExcludePromptAn dLiterals gives the text only from its text property.

              Comment

              Working...