DP_ValidateIP CFML Custom Tag

Download | Revision History | Usage

This ColdFusion 4.5+ CFML custom tag provides two abilities:

  • Simple validation to determine if an abitrary value is an IP address.
  • Complex mask/wildcard based validation. This can be used, for example, to test if a particular IP is within a certain range or group. Multiple masks may be used to create complex validations.

Download back to top

This tag requires Macromedia's Cold Fusion development enviroment, version 4.1 or later. All downloads and code are covered by our Source Code Policy.

DP_ValidateIP.zip, 2.16kb Zip-format Archive

Revision History back to top

July 10, 2001

  • Initial Release

Usage back to top

At its simplest this tag can validate whether or not any given input is an IP address. In other words in the form of four octets (decimal numbers from 0-255) separated by periods as in "xxx.xxx.xxx.xxx". The tag will not allow an IP of all zeros.

In addition one or more masks may be applied to validate if an IP is within a range, or set of IPs. Masks are created in the IP format of x.x.x.x where "x" can be any of the following:

  • A number: This will only match that exact number.
  • An asterisk ("*"): This will match any positive number from 0-255.
  • A question mark ("?") or a number containing question marks: Question marks will match any single digit from 0-9. So that "3?" will match 30-39.
  • A range: For example "10-20" will match any number in that range inclusive.

Some example masks are: "192.168.10?.*", "205.*.*.*", "192.168.12.1-100", "*.*.*.1".

Some notes about the tag:

  • The tag does not perform any clean-up on on the input value. This means, for example, that a valid IP which ends with a space will fail. It is suggested that you trim your input.
  • It is assumed that masks will be created by the developer and fundamentally static. For this reason masks are not validated. Creating invalid masks can result in errors, bad results, and general unpleasantness.
  • Although quite speedy for most uses performance will degrade if a large number of masks are used. However the tag will stop processing masks once a match is made, so sort your masks from most general to most specific for best performance.

Tag Usage

DP_ValidateIP takes three parameters:

IPRequired. The value to validate.
IPMasksOptional. One or more masks
OutputVarDefaults to "IPMatch". The variable name to contain the result.

Tag Output

The tag returns a single variable (definable via the "OutputVar attribute and defaulting to "IPMatch") with a value of "Yes" (the IP is valid or does match one of the masks) or "No" (the IP is not valid or does not match one of the masks).

Tag Examples

Assuming the value in question was contained in the variable "IPAddress" the simplest use of the tag would be:

<cfmodule template="DP_ValidateIP.cfm"
          IP = "#IPAddress#">
</cfmodule>

The following example tests to see if the IP is part of the "192.168.xxx.xxx" non-addressible subnet and sets the return value to the variable "Result":

<cfmodule template="DP_ValidateIP.cfm"
          IP = "#IPAddress#"
          IPMasks = "192.168.*.*"
          OutputVar = "Result">
</cfmodule>

The following example tests to see if the IP is between "192.168.1.1" and "192.168.1.15" or is "192.168.1.200":

<cfmodule template="DP_ValidateIP.cfm"
          IP = "#IPAddress#"
          IPMasks = "192.168.1.1-15, 192.168.1.200">
</cfmodule>

105 Current Sessions; Time: 08:36:19 06-01-2009; Tick: 531