Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Validate an IP address

Posted on 1998-08-18
5
354 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

856 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