> QUESTION 1:
> What should happen if I pass a number that is not nine digits?
Why would you pass a /string/ that's not nine digits? ;-)
> QUESTION 2:
> Should my UDFs prepend leading zeros to the beginning of any number that is
> less than nine-digits?
In this particular case, I don't think that's a good idea. If I asked
you for your social security number, I would expect "Double oh five,
ninetey-eight, twenty sixteen," not "five millions, nine hundred and
eight two thousand, and sixteen." A SSN is a series of digits, not a
number. I wouldn't accept anything other than nine digits (and maybe
some hyphens and/or other non-digit characters) as valid. However, if
we're talking about salary instead of SSN I would think padding with
zeros is perfectly reasonable.
I think having the function verify its input is reasonable if there's
a realistic possiblity of the function being called with invalid
input.
<cffunction name="formatSSN" returnType="string">
<cfargument type="string" name="ssn"/>
<cfset var result = ""/>
<cfif not ReFindNoCase('^999999999$", arguments.ssn>
<cfhrow message="SSN must be formatted 999999999."/>
</cfif>
<!--- [set result to the properly formatted SSN] --->
<cfreturn result/>
</cffunction>
But now the function has too many responsibilities. Let's refactor the
regExp into it's own funciton. (I have no idea if that regExp is
right, BTW.)
<cffunction name="formatSSN" returnType="string">
<cfargument type="string" name="ssn"/>
<cfset var result = ""/>
<cfif not isValidSSN(arguments.ssn)>
<cfhrow message="SSN must be formatted 999999999."/>
</cfif>
<!--- [set result to the properly formatted SSN] --->
<cfreturn result/>
</cffunction>
And then delegate the whole if statement to a new function.
<cffunction name="formatSSN" returnType="string">
<cfargument type="string" name="ssn"/>
<cfset var result = ""/>
<cfset assert(isValidSSN(), "SSN must be formatted 999999999.")/>
<!--- [set result to the properly formatted SSN] --->
<cfreturn result/>
</cffunction>
Of course, that's assuming that throwing an exception is the
appropriate thing to do in the case of invalid input. But depending on
your requirements you may want to input all 9s (like they do in NC
when illegal aliens apply for drivers liceneses.)
<cffunction name="formatSSN" returnType="string">
<cfargument type="string" name="ssn"/>
<cfset var result = ""/>
<cfif not isValidSSN(arguments.ssn)>
<cfreturn "999999999"/>
</cfif>
<!--- [set result to the properly formatted SSN] --->
<cfreturn result/>
</cffunction>
Okay, I got a little carried away there. Sorry. :).
Patrick
--
Patrick McElhaney
704.560.9117
http://pmcelhaney.blogspot.com
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
in the message of the email.
CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).
An archive of the CFCDev list is available at
[EMAIL PROTECTED]