Results 1 to 8 of 8

Thread: [RESOLVED] need help checking string is formatted correctly as mac address

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2013
    Posts
    122

    Resolved [RESOLVED] need help checking string is formatted correctly as mac address

    hey guys
    i have a listbox that imports a text file full of mac address's like so
    00:00:00:00:00:00
    ff:ff:ff:00:ff:ff
    the issue is when someone imports the list like this
    000000000000 blah blah
    ffffffffffff blah blah
    ff:ff:ff:ff:ff:ff blah blah

    id there an easy way to recognise this issue and rectify it whilst loading each line into the textbox

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: need help checking string is formatted correctly as mac address

    Quote Originally Posted by gerble1000 View Post
    id there an easy way to recognise this issue and rectify it whilst loading each line into the textbox
    Yes. You'll probably like the Like operator:

    Code:
    Const MAC_ADDR_FORMAT = "[0-9A-F][0-9A-F]:" & _
                            "[0-9A-F][0-9A-F]:" & _
                            "[0-9A-F][0-9A-F]:" & _
                            "[0-9A-F][0-9A-F]:" & _
                            "[0-9A-F][0-9A-F]:" & _
                            "[0-9A-F][0-9A-F]"
    
    If UCase$("01:23:45:67:89:AB") Like MAC_ADDR_FORMAT Then
        'Input string matched MAC address format
    Else
        'Input string didn't matched MAC address format
    End If
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2013
    Posts
    122

    Re: need help checking string is formatted correctly as mac address

    im liking this method but i cannot get it to work
    here i my code
    Const MAC_ADDR_FORMAT = "[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]"

    open filename for input as #1
    do while not eof(1)
    line input #1, variable8
    variable8 = left(variable8, 17)
    if (variable8) like MAC_ADDR_FORMAT then
    list2.additem variable8
    else
    msgbox(variable8 & " is the incorrect format")
    exit do
    end if
    loop
    close#1

    i taken the UCase out as it does not matter about it being upper or lower case.
    can you see where i have gone wrong
    i just get the msgbox popup with the correct mac address format
    also the mac address's i am using is all uppercase

  4. #4
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: need help checking string is formatted correctly as mac address

    Can you post a sample of the text file? The UCase$ function may be left out if you can be absolutely sure that the MAC addresses will always be uppercase.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2013
    Posts
    122

    Re: need help checking string is formatted correctly as mac address

    Quote Originally Posted by Bonnie West View Post
    Can you post a sample of the text file? The UCase$ function may be left out if you can be absolutely sure that the MAC addresses will always be uppercase.
    sure
    00:11:E6:99:41:A7 01/05/2013
    00:44:FE:FE:A1:22 02/03/2013

    pretty much like that
    the list may also have lowercase but i would allow lowercase to go through so does that mean i have to change the MAC_ADDR_FORMAT to [0-9a-fA-F]

  6. #6
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: need help checking string is formatted correctly as mac address

    Quote Originally Posted by gerble1000 View Post
    sure
    00:11:E6:99:41:A7 01/05/2013
    00:44:FE:FE:A1:22 02/03/2013

    pretty much like that
    Your code and sample text works fine for me. Have you tried stepping through your code and verifying that you're getting the expected text from the file?

    Quote Originally Posted by gerble1000 View Post
    the list may also have lowercase but i would allow lowercase to go through so does that mean i have to change the MAC_ADDR_FORMAT to [0-9a-fA-F]
    You can do it that way or you can instruct the Like operator to use case-insensitive comparisons by specifying Option Compare Text.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2013
    Posts
    122

    Re: need help checking string is formatted correctly as mac address

    my bad
    i had
    if (vairable8)
    when it should of been
    if (variable)

  8. #8
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: need help checking string is formatted correctly as mac address

    Hmm, it looks like you don't have Option Explicit enabled. Now you know what happens when you don't use it.

    If you have no more issues, you may now mark this thread Resolved!
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

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