I have been looking for these little gems recently and everytime I happen to find something new. Although, I dont blog about everything I find, this one is worth it I think.

Normally in order to validate an IP address, we would use some kind of regular expression but Coldfusion has an inbuilt class for validating IP addresses. Both the v4 and v6 addresses. Not an IP expert, have a look here for an explanation of what they exactly mean. So, here is the code :-


<cfset objIP = createObject("java","coldfusion.util.IPAddressUtils") />
<cfset ip = "127.0.0.1" />
<cfset ip1 = "192.168.0.0" />
<cfset ip2 = "10.0.0.1" />
<cfdump var = "#objIp.validateIPAdress(ip)#">
<cfdump var = "#objIp.validateIPv4Address(ip1)#">
<cfdump var = "#objIp.validateIPv6Address(ip2)#">

Notice that the validateIPAdress has only one ‘d’ and the other 2 functions have 2 d’s. Looks like this is a deprecated class but atleast worth knowing that it’s there.

There are 3 more methods available that can be used. isLocalHost(_ip_), getLocalHostIP() and prepareLocalHostIPForPort(). The usage for these is self-explanatory.