Results 1 to 23 of 23

Thread: [2008] If string has three dashes then....

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Posts
    360

    [2008] If string has three dashes then....

    How could I do an if statment such as:

    Code:
    if pnum.contains 3 "-" then
    msgbox "contains three dashes!"
    else
    msgbox "n/a"
    end if
    Understand?

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] If string has three dashes then....

    Split the string by the delimiter ("-") and count the length of the array.

    There may be a faster way.

    Cue faster way...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Posts
    360

    Re: [2008] If string has three dashes then....

    Quote Originally Posted by mendhak
    Split the string by the delimiter ("-") and count the length of the array.

    There may be a faster way.

    Cue faster way...

    Or how about a statement for if a string contains any letters?

    If str.contains letters then
    msgbox "contains numbers"
    else
    msgbox "contains letters"
    end if

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Posts
    360

    Re: [2008] If string has three dashes then....

    Or a statment like.

    If str contains 13 numbers than

    This would would be best...

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] If string has three dashes then....

    How about you tell us clearly what it is you want to examine this string for, because everything you've posted so far is different. If we don't know what you want then we can't tell you how to get it. BE CLEAR! For instance, would nay string with 13 digits do, even if it had 5000 more digits and 10000 letters in it too? You see what I'm saying? You need to be SPECIFIC about what is and isn't valid.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Posts
    360

    Re: [2008] If string has three dashes then....

    I simply need to know if the str contains no more and no less than 13 numbers.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] If string has three dashes then....

    Yes, but why?

    Are you trying to validate a phone number, for example?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Posts
    360

    Re: [2008] If string has three dashes then....

    Quote Originally Posted by mendhak
    Yes, but why?

    Are you trying to validate a phone number, for example?
    yes.

  9. #9
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2008] If string has three dashes then....

    why don't you split the string on space (" ") which is the end of word and then check the each word to see if it is a number.

    vb Code:
    1. dim words() as String = sentence.split(new Char() {" "c})
    2. for each str as String in words
    3.   if isNumeric(str) then counter += 1
    4.   if counter = 13 then exit for
    5. next str

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] If string has three dashes then....

    Use a regular expression.

    http://regexlib.com/DisplayPatterns....&cattabindex=2

    You can use one or make your own.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] If string has three dashes then....

    Quote Originally Posted by thud
    yes.
    Did I mention that you needed to be clear? This:

    1x2x3x4x5x6x7x8x9x0x1x2x3

    would pass your test but it's hardly a valid phone number, is it?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Hyperactive Member
    Join Date
    Jan 2006
    Posts
    395

    Re: [2008] If string has three dashes then....

    thanks schnookumz

    + rep for you.

  13. #13
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: [2008] If string has three dashes then....

    This is how we checked for a valid phone number in a number of school projects. We stored it in a module for utility functions.

    Code:
        ' Method name: isPhone
        ' Purpose: determine if a string is a valid phone number
        ' Parameters: a string representing a phone number
        ' Return: boolean indicating validity
           Public Function isPhone(ByVal PhoneNumber As String) As Boolean
            Try
                ' Phone #'s are expected in the format (###) ###-####
                If PhoneNumber.Length = 14 Then
                    If PhoneNumber.StartsWith("(") And _
                        IsNumeric(PhoneNumber.Substring(1, 3)) And _
                        PhoneNumber.Substring(4, 2) = ") " And _
                        IsNumeric(PhoneNumber.Substring(6, 3)) And _
                        PhoneNumber.Substring(9, 1) = "-" And _
                        IsNumeric(PhoneNumber.Substring(10, 4)) Then
                        Return True
                    End If
                End If
    
                Return False
            Catch ex As Exception
                MsgBox("An error occured in mdlUtilities. Method: isPhone. Error: " & ex.Message)
                Return False
            End Try
        End Function
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

  14. #14
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008] If string has three dashes then....

    RANTING

    Phone number validation?

    3146354494
    314-635-4494
    314.635.4494
    (314) 635-4494

    and what about extensions.
    314-635-4494 ext 1234
    314-635-4494 x 1234

    MY OPINION is that phone numbers should be free form text, unless the data is to be used by some machine i.e. autodialer. At most I only verify that there are some minimum number of digits.

    If I have a format I prefer I might display the number as such.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  15. #15
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: [2008] If string has three dashes then....

    MORE RANTING

    What if the guy's British?

    +(011) 44 2088784765
    (VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.

  16. #16
    Fanatic Member Clanguage's Avatar
    Join Date
    Jan 2008
    Location
    North Carolina
    Posts
    659

    Re: [2008] If string has three dashes then....

    Or French?
    011 33 144543171
    CLanguage;
    IF Post = HelpFull Then
    RateMe
    Else
    Say("Shut UP")
    End If
    DotNet rocks
    VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?

  17. #17
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008] If string has three dashes then....

    My point exactly. I can't believe I agree with TS
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  18. #18
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: [2008] If string has three dashes then....

    Quote Originally Posted by dbasnett
    My point exactly. I can't believe I agree with TS
    I know. I'm frightened too.
    (VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.

  19. #19
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008] If string has three dashes then....

    it is frightening that you agree with yourself. i crack me up sometimes
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  20. #20
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: [2008] If string has three dashes then....

    It was a criteria of the projects, that we provided a function to validate various data entries. The phone number was required to be in that format. It could be changed to work for any format or language. Basically I was only providing an example of how we did it.
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

  21. #21
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: [2008] If string has three dashes then....

    Quote Originally Posted by CoachBarker
    It was a criteria of the projects, that we provided a function to validate various data entries. The phone number was required to be in that format. It could be changed to work for any format or language. Basically I was only providing an example of how we did it.
    In that case, your simplest method would be to restrict the user's input. Instead of having a phone number text box, have three text boxes - one for area code and one for the next three digits and one for the last four. Make them all require numeric entries and give them a max length of 3, 3, 4. Then concatenate those three boxes together and add the dashes in order to get the phone number.

    That's way simpler than trying to validate a free form box.
    (VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.

  22. #22
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008] If string has three dashes then....

    Hey coach, I didn't mean to sound as if I was picking on you, I guessed that it was a 'requirement'. My problem is with the people that have those requirements. My experience has been that they are typically personal likes / dislikes, but if it is your dime have a blast.

    Can you imagine the discussions about DNS and names? There was actually thought of restricting labels to 24 bytes and storing DNS records in binary. Some would say that the DNS requirements don't meet the needs of today.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  23. #23
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2008] If string has three dashes then....

    Quote Originally Posted by CoachBarker
    This is how we checked for a valid phone number in a number of school projects. We stored it in a module for utility functions.

    Code:
        ' Method name: isPhone
        ' Purpose: determine if a string is a valid phone number
        ' Parameters: a string representing a phone number
        ' Return: boolean indicating validity
           Public Function isPhone(ByVal PhoneNumber As String) As Boolean
            Try
                ' Phone #'s are expected in the format (###) ###-####
                If PhoneNumber.Length = 14 Then
                    If PhoneNumber.StartsWith("(") And _
                        IsNumeric(PhoneNumber.Substring(1, 3)) And _
                        PhoneNumber.Substring(4, 2) = ") " And _
                        IsNumeric(PhoneNumber.Substring(6, 3)) And _
                        PhoneNumber.Substring(9, 1) = "-" And _
                        IsNumeric(PhoneNumber.Substring(10, 4)) Then
                        Return True
                    End If
                End If
    
                Return False
            Catch ex As Exception
                MsgBox("An error occured in mdlUtilities. Method: isPhone. Error: " & ex.Message)
                Return False
            End Try
        End Function
    Coach, just a heads up, that function can produce horribly wrong results, so if you are using it in all school projects, then all those projects have a bug...

    Try passing a string like this to your function

    Dim myString As String = "($15) $12-10.2"

    and you will see it passes without a problem

    I have said it over and over again on this forum, IsNumeric is NOT a good way to validate numeric. It will return true when a dollar sign is present followed by at least one number. It will also return true if its a valid decimal number. So you see someone could put in weird strings like the one I show above, and your code would think it was a perfectly valid phone number.

    Here is a better method. It simply uses regex to get all numbers in a given string, and returns true if there are 10.

    Code:
        Public Function isPhone(ByVal PhoneNumber As String) As Boolean
            Try
    
                'GET JUST THE NUMBERS VIA REGEX (IE STRIP ANY AND ALL FORMATTING)
                'AND RETURN TRUE IF THERE ARE 10 DIGITS, FALSE IF THERE ARE NOT
                Return System.Text.RegularExpressions.Regex.Replace(PhoneNumber, _
                                                                    "\D+", _
                                                                    String.Empty).Length = 10
            Catch ex As Exception
                MsgBox("An error occured in mdlUtilities. Method: isPhone. Error: " & ex.Message)
                Return False
            End Try
        End Function
    Likewise, if you just wanted a function get the digits (as a string) so you could then format them accordingly, you can use the same regex method in its own function, like so:

    Code:
        Public Function GetNumbersInString(ByVal text As String) As String
            Return System.Text.RegularExpressions.Regex.Replace(text, "\D+", String.Empty)
        End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width