-
Apr 26th, 2006, 12:18 PM
#1
Thread Starter
Addicted Member
Function IsIP()
I thought I'd add a little to this community. This may have been done before but I searched and didn't see this code. It's not stellar or ground breaking but it is very useful for me.
I use this function to determine if a value passed is a valid IP address or not.
VB Code:
' Verifies if Data qualifies as an IP address
Function IsIP(IP As String) As Boolean
'Declarations
Dim tmpIP As Variant
Dim Element As Variant
Dim x As Integer, y As String
'Verify 4 octets
tmpIP = Split(IP, ".")
If UBound(tmpIP) <> 3 Then
IsIP = False
Exit Function
End If
'Verify all numerics
tmpIP = Replace(IP, ".", "")
For x = 1 To Len(tmpIP)
y = Mid(tmpIP, x, 1)
If Not IsNumeric(y) Then
IsIP = False
Exit Function
End If
Next x
'Verify octet values
tmpIP = Split(IP, ".")
For Each Element In tmpIP
If Element > 255 Then
IsIP = False
Exit Function
End If
Next
'All tests passed
IsIP = True
End Function
Changes are not permanent, but change is. {Neil Peart}
-
Apr 26th, 2006, 01:22 PM
#2
Hyperactive Member
Re: Function IsIP()
not bad code, though if it checked to see if the IP existed that would be about 10 times better
KAZAR
The Law Of Programming:
As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
__________________________________
www.startingqbasic.co.uk
-
Apr 26th, 2006, 01:53 PM
#3
Re: Function IsIP()
How about this ?
No loops
VB Code:
Private Function IsIP(StrIP As String) As Boolean
Dim Elements() As String
On Error GoTo Exit_Sub
Elements = Split(StrIP, ".")
If UBound(Elements) = 3 And IsNumeric(Replace(StrIP, ".", "")) Then
IsIP = (CInt(CByte(Elements(0))) + CByte(Elements(1)) + CByte(Elements(2)) + CByte(Elements(3))) > 0
End If
Exit Function
Exit_Sub:
IsIP = False
End Function
-
Apr 26th, 2006, 02:08 PM
#4
Hyperactive Member
Re: Function IsIP()
Still Doesn't check that it exists, does it?
Tip - Use CheckInternetConnection, with the url to check as the specified ip. if the ip exists, it should return true
KAZAR
The Law Of Programming:
As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
__________________________________
www.startingqbasic.co.uk
-
Apr 26th, 2006, 02:15 PM
#5
Re: Function IsIP()
Originally Posted by kazar
Still Doesn't check that it exists, does it?
I know, I know... the ultimate check, if you can ping it, it's valid... I know that... but sometimes you just want to check the format regardless if it exists or not.
-
Apr 26th, 2006, 02:17 PM
#6
Hyperactive Member
Re: Function IsIP()
True, but it'll be valid if it can connect
KAZAR
The Law Of Programming:
As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
__________________________________
www.startingqbasic.co.uk
-
May 24th, 2006, 07:01 PM
#7
Thread Starter
Addicted Member
Re: Function IsIP()
I think determining if the data is a valid IP address and whether it's already in use on the network are two completely different things, so I didn't include it in this code.
But since you asked, here is code that will tell you if the IP address is in use or not. I found this off of MSDN somewhere.
VB Code:
' Returns True if an IP address is available
Function IsIPAvailable(IP As String) As Boolean
Dim objWMIService
Dim colPings
Dim objStatus
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colPings = objWMIService.ExecQuery("Select * From Win32_PingStatus where Address = '" & IP & "'")
For Each objStatus In colPings
If IsNull(objStatus.StatusCode) Or objStatus.StatusCode <> 0 Then
IsIPAvailable = True
End If
Next
End Function
Last edited by kzatu; May 25th, 2006 at 09:31 AM.
Changes are not permanent, but change is. {Neil Peart}
-
May 25th, 2006, 02:40 AM
#8
Hyperactive Member
Re: Function IsIP()
Thats much better, nice code.
KAZAR
The Law Of Programming:
As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
__________________________________
www.startingqbasic.co.uk
-
May 27th, 2006, 12:10 AM
#9
Hyperactive Member
Re: Function IsIP()
Originally Posted by CVMichael
I know, I know... the ultimate check, if you can ping it, it's valid... I know that... but sometimes you just want to check the format regardless if it exists or not.
Not true. I've once set up a firewall that drops pings. Stops a few script kiddies at the very least. Would only allow incoming SSH connections from authorised hosts (and authentication only by public keys), everything else from behind it would get routed, and nothing else allowed in (besides established connections).
Just saying, don't assume that a ping verifies it's existence.
-
Feb 17th, 2007, 01:24 PM
#10
Re: Function IsIP()
Valid IP on network and valid format are two different things... such as in cases where your testing a subnet mask and not an IP.
Handle exponent/non-digit characters.
VB Code:
Public Function ValidIP(ByVal Value As String) As Boolean
Dim strOctet() As String
Dim lngPos As Long
Dim bytTemp As Byte
strOctet = Split(Value, ".")
If UBound(strOctet) <> 3 Then Exit Function
Value = Join(strOctet, "")
On Error GoTo ErrHandler
'Check for existence of non-digit characters, such as E or D
'since IsNumeric("2E2") = True, and CByte("2E2") = 200
For lngPos = 1 To Len(Value)
bytTemp = CByte(Mid(Value, lngPos, 1))
Next
'Check range of octets
For lngPos = 0 To 3
bytTemp = CByte(strOctet(lngPos))
Next
ValidIP = True
Exit Function
ErrHandler:
Err.Clear
End Function
Last edited by leinad31; Feb 17th, 2007 at 01:29 PM.
-
Feb 20th, 2007, 12:29 PM
#11
Thread Starter
Addicted Member
Re: Function IsIP()
That is a clever observation on the IsNumeric (and cbyte) function. Thanks for sharing this.
Changes are not permanent, but change is. {Neil Peart}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|