FilterMask(string, mask [, filter])
Last updated October 15, 2004
Version: 2 | Requires: ColdFusion 5 | Library: StrLib
Description:
Applies a formatting mask to a string. Valid masks are:
<UL><LI>_ character unchanged </LI><LI>0 character is unchanged for numerics and forced to zero for all others </LI><LI>9 character is unchanged for numerics and forced to empty for all others </LI><LI>a character is forced to lower case, all others are left as is </LI><LI>A character is forced to upper case, all others are left as is </LI><LI>b character is forced to lower case, numerics are forces to empty </LI><LI>B character is forced to upper case, numerics are forces to empty </LI><LI>\ following character is literal </LI></UL>
Return Values:
Returns a string.
Example:
<CFSET mask="(___)___-____">
<CFSET filter="numeric">
<CFOUTPUT>
Given str="#str#", mask="#mask#", and filter="#filter#"<BR>
The FilterMask version is #FilterMask(str, mask, filter)#
</CFOUTPUT>
Parameters:
| Name | Description | Required |
|---|---|---|
| string | String to be modified. | Yes |
| mask | See Mask description above. | Yes |
| filter | Option filter to apply before applying the mask. May be 'alpha', 'numeric', or 'alphanumeric'. Any characters not within the set specified are removed from the input before the mask is applied. | No |
Full UDF Source:
<cfscript>
/**
* Applies a filter mask to a string.
*
* @param string String to be modified. (Required)
* @param mask See Mask description above. (Required)
* @param filter Option filter to apply before applying the mask. May be 'alpha', 'numeric', or 'alphanumeric'. Any characters not within the set specified are removed from the input before the mask is applied. (Optional)
* @return Returns a string.
* @author Joshua Olson (joshua@waetech.com)
* @version 2, October 15, 2004
*/
function FilterMask(value, mask) {
var filter = ",";
var t_value = "";
var pos = 1;
var t_value_len = 0;
var character = "";
var literal = 0;
var char_at_pos = "";
var argc = ArrayLen(arguments);
if (argc EQ 2) ArrayAppend(arguments,filter);
filter = arguments[3];
t_value = value;
value = "";
if (LCase(filter) IS "alphanumeric")
t_value = REReplace(t_value, "[^[:alnum:]]", "", "ALL");
else if (LCase(filter) IS "numeric")
t_value = REReplace(t_value, "[^[:digit:]]", "", "ALL");
else if (LCase(filter) IS "alpha")
t_value = REReplace(t_value, "[^[:alpha:]]", "", "ALL");
t_value_len = Len(t_value);
for (i=1; i LTE Len(mask); i = i + 1) {
character = Mid(mask, i, 1);
if (literal)
{
value = value & character;
literal = "0";
} else
{
if (t_value_len GTE pos)
char_at_pos = Mid(t_value, pos, 1);
else
char_at_pos = "";
pos = pos + 1;
if (character IS "9") {
if (IsNumeric(char_at_pos)) value = value & Val(char_at_pos);
} else
if (character IS "0") {
value = value & Val(char_at_pos);
} else
if (character IS "_") {
value = value & char_at_pos;
} else
if (character IS "A") {
value = value & UCase(char_at_pos);
} else
if (character IS "a") {
value = value & LCase(char_at_pos);
} else
if (character IS "B") {
if (NOT IsNumeric(char_at_pos)) value = value & UCase(char_at_pos);
} else
if (character IS "b") {
if (NOT IsNumeric(char_at_pos)) value = value & LCase(char_at_pos);
} else
if (character IS "\") {
literal = 1;
pos = pos - 1;
}
else {
value = value & character;
pos = pos - 1;
}
}
}
return value;
}
</cfscript>
Search CFLib.org
Latest Additions
Dave Anderson added
iniToStruct
20 day(s) ago
Dave Anderson added
deDupeArray
20 day(s) ago
Richard added
dice
22 day(s) ago
Isaac Dealey added
getRelative
a while ago
Top Rated
backupDatabase
Rated 5.0, 22 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)
highlightAndCrop
Rated 5.0, 4 time(s)