How to verifying a variable is an IP Address...

In my Visual Basic program, I need to verify that a variable is in fact an IP address.

dim myIP
myIP = "192.168.0.1"
check = verifyIP(myIP)

Private Function verifyIP(myIP)
'Your code here will return true or false
verifyIP = true
end Function

=============

PS: I will only give points to full working code.
LVL 1
frankmorrisonAsked:
Who is Participating?
 
PaulHewsConnect With a Mentor Commented:
Also correction, the above will match substrings of the passed in string.  This will match only a full string:

Private Function IsIP(strIP As String) As Boolean
    Dim Regex As regexp
   
    Set Regex = New regexp
    Regex.Pattern = "^\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b$"
    IsIP = Regex.Test(strIP)
    Set Regex = Nothing
   
End Function
0
 
PaulHewsCommented:
This matches the range from 0.0.0.0 to 255.255.255.255   Strictly speaking many of those aren't real IP addresses.  But it correctly identifies obviously wrong strings, as in the example.

Option Explicit

Private Sub Command1_Click()
    Dim strIP As String, strIP2 As String
    strIP = "192.168.1.1"
    strIP2 = "72.333.1.1"
    Debug.Print IsIP(strIP)
    Debug.Print IsIP(strIP2)
   
   
   
   
End Sub


Private Function IsIP(strIP As String) As Boolean
    Dim Regex As regexp
   
    Set Regex = New regexp
    Regex.Pattern = "\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
    IsIP = Regex.Test(strIP)
    Set Regex = Nothing
   
End Function

0
 
dqmqCommented:
Are you testing for IP address that is actually "listening" or just one that is formatted correctly?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
frankmorrisonAuthor Commented:
PaulHews:
I am getting an error on this line "Dim Regex As regexp"
Error: "User-defined type not defined"
0
 
frankmorrisonAuthor Commented:
I am interested in correcting the formatting of the string variable
0
 
frankmorrisonAuthor Commented:
PS: I am using Visual Studio 6.0
0
 
PaulHewsCommented:
Sorry!  You need a reference to Microsoft VBScript Regular Expressions.
0
 
frankmorrisonAuthor Commented:
I am not sure what you mean when you say "this will match only a full string".  Basically, my program queries a web server for an ip address.  However, sometimes, my webserver will return something other than ip address, so I just want to validate the ip address format.  Here's some of the possibilities my web server will return:

myIP = ""
myIP2 = "error 200"
myIP3 = "192.0.1.132"

0
 
PaulHewsCommented:
>I am not sure what you mean when you say "this will match only a full string".

I meant that my first post would return true with an IP as a substring of the passed in parameter:

"this is an ip address 172.12.34.36"

The second one would return false for that, but true for a string with just the IP part:

"172.12.34.36"
0
 
frankmorrisonAuthor Commented:
I am looking for the second option, but I am getting this Error: "User-defined type not defined"
0
 
PaulHewsCommented:
You need a reference to Microsoft VBScript Regular Expressions.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.