Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

Validate an IP address

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
cliff_m
Asked:
cliff_m
1 Solution
 
KevJSLCommented:
Why don't you split it up?
0
 
anthonycCommented:
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
 
cliff_mAuthor Commented:
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
 
cliff_mAuthor Commented:
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
 
Answers2000Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now