CFLib.org – Common Function Library Project

IsIP(string)

Last updated July 17, 2001

Version: 1 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 2 time(s). Average Rating: 4.5

Description:
Performs a regex on the string and determines if it a valid IP address. The value passed must match contain four octets delimited by a period. Each octet must be a number from 1 to 255. The addresses 0.0.0.0 and 255.255.255.255 are not valid. Note This only checks the octet form of IP addresses. It does not check the binary form.

Return Values:
Returns a boolean.

Example:

view plain print about
<CFSET BADIP = "fred">
<CFSET BADIP2 = "120.120.120.0">
<CFSET IP = "216.115.108.245">
<CFOUTPUT>
Is #BADIP# valid? #IsIP(BADIP)#<BR>
Is #BADIP2# valid? #IsIP(BADIP2)#<BR>
Is #IP# valid? #IsIP(IP)#<BR>
</CFOUTPUT>

Parameters:

Name Description Required
string String to be checked. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Returns TRUE if the string is a valid IP address.
 * 
 * @param string      String to be checked. 
 * @return Returns a boolean. 
 * @author Nathan Dintenfass (nathan@changemedia.com) 
 * @version 1, July 17, 2001 
 */

function isIP(ip){
    var ii = 1;
    //make sure it is a '.' delimited list 4 long
    if(listlen(ip,".") is not 4) return false;

    //make sure each item is a number between 1 and 255
    for(ii = 1;ii lte 4;ii = ii + 1){
        if(not isnumeric(listgetat(ip,ii,".")) OR listgetat(ip,ii,"."GT 255 OR listgetat(ip,ii,"."LT 0)    return false;
    }
    //check for the special cases of 255.255.255.255 or 0.0.0.0, which is not really valid
    if(ip is "255.255.255.255" OR IP is "0.0.0.0"return false;
    return true;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
3 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson