• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

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.
0
frankmorrison
Asked:
frankmorrison
  • 5
  • 5
1 Solution
 
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
 
frankmorrisonAuthor Commented:
PaulHews:
I am getting an error on this line "Dim Regex As regexp"
Error: "User-defined type not defined"
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
PaulHewsCommented:
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
 
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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