Solved

Validate an IP address

Posted on 1998-08-18
5
357 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
[X]
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
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

734 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