Visual Basic 6 function "IsValidIPv4Address"

Go back

Below you'll find the source for the Visual Basic 6 function IsValidIPv4Address.

Attribute VB_Name = "modIsValidIPv4Address"
' This function is downloaded from:
' http://www.stefanthoolen.nl/archive/vb6-functions/
' 
' You may freely distribute this file but please leave all comments, including this one, in it.
' 
' @Author Stefan Thoolen <mail@stefanthoolen.nl>

Option Explicit

''
' Checks if an IP address is valid according to the IPv4 standards
' @param    String   The IP address
' @return   Boolean  True if it's valid, false if it's not
' @author   Stefan Thoolen <mail@stefanthoolen.nl>
Public Function IsValidIPv4Address(ByVal ip As String) As Boolean
    On Error GoTo IsValidIPv4Address_Error
    Dim addr As Variant, i As Integer, cnt As Integer
    addr = Split(ip, ".")
    
    If IsArray(addr) Then
        For i = LBound(addr) To UBound(addr)
            cnt = cnt + 1
            addr(i) = CByte(addr(i)) ' Gives an error if the number isn't between 0 and 255 :-)
        Next
        IsValidIPv4Address = (cnt = 4) ' Gives false if cnt is something else then 4
    End If
IsValidIPv4Address_Error:
End Function