Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

ColdFusion, Strings, Masks and Filters

Rated 4.05 (Ratings: 4) (Add your rating)

Log in to add a comment
(8 comments so far)

Want more?

 
Picture of joshua

Joshua Olson

Member info | Full bio

User since: September 21, 2000

Last login: September 21, 2000

Articles written: 3

Purpose: Applies generic masks to filtered strings
Method: brute force

Attributes:
  • value - the value to which we apply the mask
  • result - variable in which to store the answer, defaults to "out"
  • mask - mask to apply to the value
  • filter - prefilter of value

No parameters are required, though not specifying anything is boring.

filter accepts "alpha", "numeric", "alphanumeric" and is a prefilter to the string. For example, the string "a1b2-c3c4" would be processed as the following with the different filters:

  • none: "a1b2-c3d4"
  • alpha: "abcd"
  • numeric: "1234"
  • alphanumeric: "a1b2c3d4"

The filtered string would *then* be applied to the mask. The mask entities are as follows:

  • _ character unchanged
  • 0 character is unchanged for numerics and forced to zero for all others
  • 9 character is unchanged for numerics and forced to empty for all others
  • a character is forced to lower case, all others are left as is
  • A character is forced to upper case, all others are left as is
  • b character is forced to lower case, numerics are forces to empty
  • B character is forced to upper case, numerics are forces to empty
  • \ following character is literal

Usage:

&lt;cf_filtermask value="123abc" mask="___ ___" <br>result="license_plate"&gt;
returns: 123 abc

&lt;cf_filtermask value="ab 1sf 2 sd 3 s 45" mask="\_0+\0b999999" <br>filter="numeric"&gt;
returns: _1+0345

&lt;cf_filtermask value="APPL9E" mask="Bbbbbb \B"&gt;
returns: Apple B

&lt;cf_filtermask value="706-555-1212ext123" mask="(___)___-____"<br>
filter="numeric"&gt;

and
&lt;cf_filtermask value="7065551212" mask="(___)___-____" <br>filter="numeric"&gt;
return: (706)555-1212
 

The last example is why I made it in the first place.

<cfsilent>
<cfparam name="attributes.value" default="">
<cfparam name="attributes.result" default="out">
<cfparam name="attributes.mask" default="">
<cfparam name="attributes.filter" default="">

<cfset mask = attributes.mask>
<cfset t_value = attributes.value>

<cfif attributes.filter IS "numeric">
  <cfset t_value = REReplace(t_value, "[^[:digit:]]", "", "ALL")>
</cfif>

<cfif attributes.filter IS "alpha">
  <cfset t_value = REReplace(t_value, "[^[:alpha:]]", "", "ALL")>
</cfif>

<cfif attributes.filter IS "alphanumeric">
  <cfset t_value = REReplace(t_value, "[^[:alnum:]]", "", "ALL")>
</cfif>

<cfset value = "">
<cfset pos = "1">
<cfset literal = "0">
<cfloop index="i" from="1" to="#Len(mask)#">
  <cfset character = Mid(mask, i, 1)>
  <cfif literal>
    <cfset value = value & character>
    <cfset literal = "0">
  <cfelse>
    <cfif Len(t_value) GTE pos>
      <cfset char_at_pos = Mid(t_value, pos, 1)>
    <cfelse>
      <cfset char_at_pos = "">
    </cfif>
    <cfif character IS "9">
      <cfif isNumeric(char_at_pos)>
        <cfset value = value & Val(char_at_pos)>
      </cfif>
      <cfset pos = pos + "1">
    <cfelseif character IS "0">
      <cfset value = value & Val(char_at_pos)>
      <cfset pos = pos + "1">
    <cfelseif character IS "_">
      <cfset value = value & char_at_pos>
      <cfset pos = pos + "1">
    <cfelseif Find("A", character)>
      <cfset value = value & UCase(char_at_pos)>
      <cfset pos = pos + "1">
    <cfelseif Find("a", character)>
      <cfset value = value & LCase(char_at_pos)>
      <cfset pos = pos + "1">
    <cfelseif Find("b", character)>
      <cfif NOT isNumeric(char_at_pos)>
        <cfset value = value & LCase(char_at_pos)>
      </cfif>
      <cfset pos = pos + "1">
    <cfelseif Find("B", character)>
      <cfif NOT isNumeric(char_at_pos)>
        <cfset value = value & UCase(char_at_pos)>
      </cfif>
      <cfset pos = pos + "1">
    <cfelseif Find("\", character)>
      <cfset literal = "1">
    <cfelse>
      <cfset value = value & character>
    </cfif>
  </cfif> <!--- if literal --->
</cfloop>

<cfset t = SetVariable("caller.#attributes.result#", value)>
</cfsilent>

Good idea, but....

Submitted by judithtaylor on May 20, 2001 - 12:29.

In your last example, you have the input value of a phone number with an extension. The returned output, while nicely formatting the phone number into a recognized format for US numbers, loses the extension. Also, what about non-US phone numbers where the format is not (nnn) nnn-nnnn?

login or register to post comments

Non-us Phone Numbers

Submitted by joshua on May 20, 2001 - 15:28.

login or register to post comments

Non-US Phone Numbers

Submitted by joshua on May 20, 2001 - 15:30.

If you want to use non US phone number formats, you'd change the mask to something like: "_ __ ___-___ __" or whatever you want. The underscores represent digits, and any spaces or other characters you use are simply inserted between the digit! Does this answer your question?

login or register to post comments

Non-US Phone Numbers

Submitted by judithtaylor on May 20, 2001 - 16:56.

Yep. :o) Thanks!

login or register to post comments

nothing displayed

Submitted by underlord on February 13, 2004 - 08:24.

When I use this tag I don't get an error, but where there is suppose to be a number nothing is displayed? Did anyone else have this problem? Is the code above correct, or do I need to make some changes to it before it will work? Thanks

login or register to post comments

It doesn't display a result

Submitted by joshua on February 13, 2004 - 08:28.

It returns the result as a variable in the caller scope. The default variable is "out". So call the CT and then add

<cfoutput>#out#</cfoutput>

(omit the cfoutput's if you're already within a cfoutput block, of course)

login or register to post comments

It doesn't display a result

Submitted by underlord on February 13, 2004 - 08:53.

OK, I didn't see that variable, it works now. Thanks

login or register to post comments

Thanks Joshua

Submitted by wolffy on January 20, 2005 - 15:37.

This tag is great. I can't believe Macromedia does not have a function for formatting strings. I though Allaire had one. I just came back to ColdFusion after a long journey down a different road. Who do we send our complaints to at Macromedia? Thanks, Steven.

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.