Here is a formatting tag we use for several things. Stored in CustomTags as
format.cfm. It is an old tag so please don't critique the details of the
coding.
Shawn McKee
<!---
Purpose: Format a field with the given delimiter
Attributes:
type="ssn | phone | zip | cc" -required
value="[value to be formatted]" -required
var="[variable where you want the output stored]" -default
"formatVar"
parents="yes | no" -default "yes" -notes specifies if you
want parentheses around a phone #
delimiter="[value to use as a delimter, i.e .,-,(,)]"
-default "-"
maskcc="yes | no" -default "yes" will put in '*' for first
segments of cc number
Error Throws:
If a required attribute isn't specified then an application
throw is done.
To catch the error: put the tag in a try/catch format and
then process the error as you like.
Usage Notes:
if installed under custom tags directory:
<cf_format type="ssn|phone|zip" value="#form.phonenumber#"
[var="#somevariable#"] [parents="yes|no"] [delimter="#delimiter"]>
if installed in relative directory to your application:
<cfmodule template="#path#/format.cfm" type="ssn|phone|zip"
value="#form.phonenumber#" [var="#somevariable#"] [parents="yes|no"]
[delimter="#delimiter"]>
if this custom tag has thesame name as another tag in
custom tags directory:
<cfmodule name="#dir#.#dir2#.format" type="ssn|phone|zip"
value="#form.phonenumber#" [var="#somevariable#"] [parents="yes|no"]
[delimter="#delimiter"]>
Misc. Notes:
For dynamic variables you must only use local variables.
Other scoped variables will cause a run-time error.
i.e, <cf_format type="ssn" value="#form.phonenumber#"
var="form.phonenumber"> - this will cause an error.
Modification Log:
Date Author Change
========== ============= ====================================
--->
<!--- Set default values on attributes --->
<cfparam name="attributes.type" default=-1>
<cfparam name="attributes.value" default=-1>
<cfparam name="attributes.var" default="formatVar">
<cfparam name="attributes.parents" default="yes">
<cfparam name="attributes.delimiter" default="-">
<cfparam name="attributes.maskcc" default="yes">
<!---
Check required attributes for validity
and if it not valid then throw an error
to the calling page
--->
<cfif trim(attributes.type) is -1>
<cfthrow message="Format.cfm: Type is a required attribute.">
</cfif>
<cfif trim(attributes.value) is -1>
<cfthrow message="Format.cfm: Value is a required attribute.">
</cfif>
<!--- Strip all whitespace form the variable --->
<cfset attributes.value = trim(attributes.value)>
<!--- determine the type and do the proper format --->
<cfswitch expression=#lcase(attributes.type)#>
<cfcase value="ssn">
<cfset attributes.value = insert(trim(attributes.delimiter),
attributes.value, 3)>
<cfset attributes.value = insert(trim(attributes.delimiter),
attributes.value, 6)>
</cfcase>
<cfcase value="phone">
<cfif len(trim(attributes.value)) gt 7>
<cfif attributes.parents is "yes">
<cfset attributes.value = insert("(",
attributes.value, 0)>
<cfset attributes.value = insert(") ",
attributes.value, 4)>
<cfset attributes.value =
insert(trim(attributes.delimiter), attributes.value, 9)>
<cfelse>
<cfset attributes.value =
insert(trim(attributes.delimiter), attributes.value, 3)>
<cfset attributes.value =
insert(trim(attributes.delimiter), attributes.value, 7)>
</cfif>
</cfif>
</cfcase>
<cfcase value="zip">
<cfif len(trim(attributes.value)) gt 5>
<cfset attributes.value =
insert(trim(attributes.delimiter), attributes.value, 5)>
</cfif>
</cfcase>
<cfcase value="cc">
<cfif #attributes.maskcc# eq "yes" AND len(trim(attributes.value))
gt 4>
<cfset endloop = len(trim(attributes.value)) - 4>
<cfset attributes.value =
removechars(attributes.value,1,(len(trim(attributes.value))-4))>
<cfloop index="maskloop" from="1" to="#endloop#">
<cfset attributes.value =
insert('*',attributes.value,0)>
</cfloop>
</cfif>
<cfif len(trim(attributes.value)) EQ 16>
<cfset attributes.value = insert(trim(attributes.delimiter),
attributes.value, 12)>
<cfset attributes.value = insert(trim(attributes.delimiter),
attributes.value, 8)>
<cfset attributes.value = insert(trim(attributes.delimiter),
attributes.value, 4)>
<cfelseif len(trim(attributes.value)) EQ 15>
<cfset attributes.value = insert(trim(attributes.delimiter),
attributes.value, 10)>
<cfset attributes.value = insert(trim(attributes.delimiter),
attributes.value, 4)>
<cfelseif len(trim(attributes.value)) EQ 13>
<cfset attributes.value = insert(trim(attributes.delimiter),
attributes.value, 10)>
<cfset attributes.value = insert(trim(attributes.delimiter),
attributes.value, 7)>
<cfset attributes.value = insert(trim(attributes.delimiter),
attributes.value, 5)>
</cfif>
</cfcase>
<cfdefaultcase>
<cfthrow message="attributes.type is not a valid value.">
</cfdefaultcase>
</cfswitch>
<!--- Assign the newly formatted variable to the user defined variable --->
<cfset "caller.#attributes.var#" = attributes.value>
-----Original Message-----
From: Burcham, Steve [mailto:[EMAIL PROTECTED]]
Sent: Friday, May 17, 2002 2:27 PM
To: CF-Talk
Subject: Phone Number Format
I have a column in a SQL database called COMP_PHONE, it is a char data type.
Right now the phone number is in the database as 1234567890.
I want to output this as (123) 456-7980 in my application. I have tried
using NumberFormat to no avail. I get an error message when I run the code
stating the format is invalid.
Here is the code I was using:
#NumberFormat(COMP_PHONE, '(999)999-9999')#
Any suggestions would be appreciated.
Thank you.
Steve Burcham
Webmaster - Field Support Team
RDO Equipment Co.
Phone (701) 239-8755
<mailto:[EMAIL PROTECTED]> [EMAIL PROTECTED]
______________________________________________________________________
Your ad could be here. Monies from ads go to support these lists and provide more
resources for the community. http://www.fusionauthority.com/ads.cfm
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists