Validate IP Address Natively with Coldfusion

On February 21, 2008, in ColdFusion, by Anuj Gakhar

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 = "" />
<cfset ip1 = "" />
<cfset ip2 = "" />
<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.

Tagged with:  

6 Responses to Validate IP Address Natively with Coldfusion

  1. radek says:

    Of course this won’t work in two cases:
    1. When access to internal CF8 classes is disabled
    2. In most shared environments

  2. Anuj Gakhar says:

    Thats true. Thats true for any CreateObject calls with Java.

  3. Anuj Gakhar says:

    I meant to say not all Java createObject calls but the ones that use internal CF classes.

  4. I have been trying to figure out how to implement this kind of a script for months. I have had no luck. Could you maybe explain in a little more detail for someone who is still learning a lot. Thanks greatly!

  5. Sami Hoda says:

    It thinks is a valid IP. Looks like the validation is weak.

  6. Sami Hoda says:

    Not to mention:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2011 Anuj Gakhar