B4J Code Snippet IP Address validation using RegEx

B4X:
'Tests given string if it looks like an ipv4 address
Public Sub IsValidIPv4Address(IPAddress As String) As Boolean
    Return Regex.IsMatch("^(([01]?\d\d?|2[0-4]\d|25[0-5])\.){3}([01]?\d\d?|2[0-4]\d|25[0-5])$", IPAddress)   
End Sub

'Tests given string if it looks like an ipv6 address
Public Sub IsValidIPv6Address(IPAddress As String) As Boolean
    Return Regex.IsMatch("^([0-9a-f]{1,4}:){7}([0-9a-f]){1,4}$", IPAddress)
End Sub
 
Top