Solved

Validate an IP address

Posted on 1998-08-18
5
345 Views
Last Modified: 2010-04-30
how do you validate an IP address. I know that the range is from 0.0.0.0 to 255.255.255.255. I've tried Case "0.0.0.0" to "255.255.255.255" but it allows values of 255.444.555.66 and not 26.23.21.7. Any help would be appreciated.
0
Comment
Question by:cliff_m
5 Comments
 

Expert Comment

by:KevJSL
ID: 1429598
Why don't you split it up?
0
 
LVL 6

Expert Comment

by:anthonyc
ID: 1429599
select case does not work, because you are attempting to validate a string.  anything that starts with 255 will fall within the range.  This is what you need to do.

1)  break the ip address up into it's 4 components (separate the string based on the

public sub IPComponents(szIP as string, nIP() as integer)
   dim nI as integer
   dim nJ as integer
   dim nCt as integer

   redim nIP(0 to 3) as integer

   nI = instr(szip, ".")
   nj = 1
   do until nI = 0
      nIP(nct) = cint(mid$(szip, nj, ni - nj))
      nct = nct + 1
      nj = ni
      ni = instr(ni + 1, szip, ".")
   loop
end sub

Now validate the 4 components

dim nIP() as integer

call ipcomponents("255.444.555.66", nip())

for nI = 0 to 3
  if nip(ni) < 0 or nip(ni) > 255 then
    msgbox "Not valid"
  end if
next ni
               
0
 

Author Comment

by:cliff_m
ID: 1429600
I have tried this code as is and it apparently does not reject the test ip address "255.444.555.66". Perhaps I am doing it wrong. Also I try following the code and I think I understand it for the most part, however I am confused by the nct in the following code:


      nIP(nct) = cint(mid$(szip, nj, ni - nj))
      nct = nct + 1

what function does it have?
Please forgive my ignorance, for I am a vb newbie.

Cliff Martin
0
 

Author Comment

by:cliff_m
ID: 1429601
I have tried this code as is and it apparently does not reject the test ip address "255.444.555.66". Perhaps I am doing it wrong. Also I try following the code and I think I understand it for the most part, however I am confused by the nct in the following code:


      nIP(nct) = cint(mid$(szip, nj, ni - nj))
      nct = nct + 1

what function does it have?
Please forgive my ignorance, for I am a vb newbie.

Cliff Martin
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
ID: 1429602
Try this code instead, it checks
1. The number of dots
2. Each element is in range 0 to 255
3. Each element is all numeric characters

To use call ValidIP (example calls to this function are in DisplayValidIP), and Command_Click1 in my example calls this.  
ValidIPComponent is a helper function which is used by ValidIP (it checks a subcomponent of the IP address)


' Helper function for ValidIP, simply validates every character in a string
' is numeric, and the value is in the range 0 to 255 (VAL is indicate for
' this task).  Will return TRUE if valid, FALSE if not
Public Function ValidIPComponent(comp$) As Boolean
If (Len(comp$) = 0) Then
    ' empty component
     ValidIPComponent = False
Else
    ' Get length of string
    ll = Len(comp$)
    AllDigits = True
   
    For x = 1 To ll
        ch$ = Mid$(comp$, x, 1)
        If (ch$ < "0") Or (ch$ > "9") Then
            AllDigits = False
        End If
    Next x
   
    If AllDigits Then
        ' IP contains all numbers, check it's in range
        v = Val(comp$)
        If (v >= 0) And (v <= 255) Then
            ValidIPComponent = True
        Else
            ValidIPComponent = False
        End If
    Else
        ' IP contains bad characters
        ValidIPComponent = False
    End If
   
End If

End Function
'
' This function validates an IP address passed to it as a string
' It will return TRUE if the IP address is valid, FALSE if not
'
Public Function ValidIP(ip$) As Boolean

' Check for empty string
If (Len(ip$) = 0) Then
     ValidIP = False
Else
    ' Get length of string
    ll = Len(ip$)

   
    ' check it has 3 dots and only 3 dots, count dots
    dotcount = 0
    For x = 1 To ll
        If Mid$(ip$, x, 1) = "." Then dotcount = dotcount + 1
    Next
   
    If (dotcount = 3) Then
        ' correct number of dots, now check each sub component
        Dim comp$(4)
       
        ' make a temp string with a final dot, that way we can assume
        ' each item is ended with a dot
        temp$ = ip$ + "."
        ' start with first component
        pos = 1
       
        For x = 1 To 4
            ' get a component starting from previous pos
            nextdot = InStr(Mid$(temp$, pos), ".")
            comp$(x) = Mid$(temp$, pos, nextdot - 1)
            pos = pos + nextdot
        Next x
       
        ' we have now have each of the 4 components in each element is in
        ' the range 0 to 255, and every char in the component is a
        ' number (VAL will accept spaces, hex numbers etc)
        If ValidIPComponent(comp$(1)) And ValidIPComponent(comp$(2)) And ValidIPComponent(comp$(3)) And ValidIPComponent(comp$(4)) Then
            ValidIP = True
        Else
            ValidIP = False
        End If
       
    Else
        ' incorrect number of dots, therefore not valid
        ValidIP = False
    End If
End If

End Function



'
' This function displays a message box indicating if an IP address
' passed to it is valid or not
Public Sub DisplayValidIP(ip$)

If (ValidIP(ip$)) Then
    MsgBox Chr$(34) + ip$ + Chr$(34) + " is a valid IP address"
Else
    MsgBox Chr$(34) + ip$ + Chr$(34) + " is not a valid IP address"
End If

End Sub


Public Sub Command1_Click()
' some good IPs
DisplayValidIP "1.2.3.4"
' a bad IP (bad values)
DisplayValidIP "111.222.333.444"
' a bad IP (bad strings)
DisplayValidIP "111. 22.333.444"
' a bad IP (too many dots)
DisplayValidIP "1.2.3.4.5.6.7"
' a bad IP (too few dots)
DisplayValidIP "1.2"
' a bad IP ( right number of dots, missing element)
DisplayValidIP "1..3"
End Sub

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now