Using regular expressions with the CFPARAM tag in ColdFusion

Readability

Using regular expressions with the CFPARAM tag in ColdFusion

In a pre­vi­ous exam­ple, “Set­ting the map type on a Google Map using Cold­Fu­sion”, we saw how you could set the map type on a Google Map in Cold­Fu­sion by using the <CFMAP> tag and set­ting the type attribute to “map”, “satel­lite”, “hybrid”, “ter­rain”, or “earth”.

The fol­low­ing exam­ple shows how you can spec­ify a reg­u­lar expres­sion for the <CFPARAM> tag in Cold­Fu­sion by set­ting the type attribute to “regex” or “regular_​expression” and then spec­i­fy­ing the pattern attribute to a reg­u­lar expres­sion that the para­me­ter must match.


The pre­vi­ous reg­u­lar expres­sion basi­cally enforces that the #URL.type# vari­able is either “map”, “satel­lite”, “hybrid”, “ter­rain”, or “earth” (the “(?i)” at the begin­ning of the RegEx pat­tern spec­i­fies that the val­ues are case insen­si­tive, so the <CFPARAM> tag will accept “map”, “Map”, or “MAP”). If the spec­i­fied #URL.type# doesn’t match the spec­i­fied pat­tern, an error is thrown. You can catch the thrown error using the <CFTRY> and <CFCATCH> tags, as seen in the fol­low­ing example:


    
    
        
        
    

Or you could val­i­date a value against a list of enums using the listContains() func­tion or listContainsNoCase() func­tion, as seen in the fol­low­ing example:




    

URL.mapType must be one of the following supported values: "map", "satellite", "hybrid", "terrain", or "earth".

In a previous example, “Setting the map type on a Google Map using ColdFusion”, we saw how you could set the map type on a Google Map in ColdFusion by using the <CFMAP> tag and setting the type attribute to “map”, “satellite”, “hybrid”, “terrain”, or “earth”.

The following example shows how you can specify a regular expression for the <CFPARAM> tag in ColdFusion by setting the type attribute to “regex” or “regular_expression” and then specifying the pattern attribute to a regular expression that the parameter must match.

<cfparam name="URL.type"
        default="map"
        type="regex"
        pattern="((?i)map|satellite|hybrid|terrain|earth)" />

The previous regular expression basically enforces that the #URL.type# variable is either “map”, “satellite”, “hybrid”, “terrain”, or “earth” (the “(?i)” at the beginning of the RegEx pattern specifies that the values are case insensitive, so the <CFPARAM> tag will accept “map”, “Map”, or “MAP”). If the specified #URL.type# doesn’t match the specified pattern, an error is thrown. You can catch the thrown error using the <CFTRY> and <CFCATCH> tags, as seen in the following example:

<cftry>
    <cfparam name="URL.mapType" default="map" type="regex" pattern="((?i)map|satellite|hybrid|terrain|earth)" />
    <cfcatch type="expression">
        <cfdump var="#cfcatch.detailMessage#" />
        <cfabort />
    </cfcatch>
</cftry>

Or you could validate a value against a list of enums using the listContains() function or listContainsNoCase() function, as seen in the following example:

<cfparam name="URL.mapType"
        default="map"
        type="string" />
 
<cfif NOT listContainsNoCase("map,satellite,hybrid,terrain,earth", URL.mapType)>
    <p><code>URL.mapType</code> must be one of the following supported values: "map", "satellite", "hybrid", "terrain", or "earth".</p>
    <cfabort />
</cfif>

2 thoughts on “Using regular expressions with the CFPARAM tag in ColdFusion”

Leave a Reply

Your email address will not be published.