tirsdag, mai 02, 2006

How to validate an IPaddress

Usage:
if(IsValidIPAddress("127.0.0.1") = True) Then
messagebox.show("The ipaddress is valid.")
else
messagebox.show("The ipaddress is invalid. This might be hostname, try to resolve it.")
end if


Public Function IsValidIPAddress(ByVal str As String) As Boolean
Dim r As Regex = New Regex("((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)")
Dim m As Match = r.Match(str)
If m.Success = True Then
Return True
End If
Return False
End Function