I would probably make the default for max longer than 1 -- 20 or 50 maybe...
20 is the default length for html form fields. 50 is the default size of MS
SQL Server varchar columns.
I would probably also use "" instead of "[not provided]" as the default for
fieldname and equalfieldname -- if len(trim(var)) is 0 then you know it
wasn't provided.
Doesn't look like this should be tough to convert to a UDF and I think it
would likely make it faster and easier to use...
<cfscript>
function validateFormField(fName,fType,fValue) {
var required = false;
var fMin = "1";
var fMax = "50";
var eqVal = "";
var eqField = "";
var errormessage = "";
// allow optional arguments
if (arraylen(arguments) gte 4) { required = arguments[4]; }
if (arraylen(arguments) gte 5) { fMin = arguments[5]; }
if (arraylen(arguments) gte 6) { fMax = arguments[6]; }
if (arraylen(arguments) gte 7) { eqVal = arguments[7]; }
if (arraylen(arguments) gte 8) { eqField = arguments[8]; }
// validate for the given field type
switch (fType) {
case "string": { // check length of string for minimum and maximum
if (len(attributes.fieldValue) lt attributes.min) {
errorMessage = "#attributes.fieldName# is less than the minimum
#attributes.min# characters."; }
else if (len(attributes.fieldValue) gt attributes.max) {
errorMessage = "#attributes.fieldName# is more than the maximum
#attributes.max# characters."; }
} break;
}
}
return errormessage;
}
</cfscript>
<cfset variables.error = validateFormField(xxx,xxx,xxx)>
You need a break; statement at the end of each case clause otherwise it will
continue processing with the next case in the list. In any event -- that
should get you going. :)
s. isaac dealey 954-776-0046
new epoch http://www.turnkey.to
lead architect, tapestry cms http://products.turnkey.to
certified advanced coldfusion 5 developer
http://www.macromedia.com/v1/handlers/index.cfm?ID=21816
> For some reason, code after the HTML Horizontal Line Break
> was omitted.
> Here it is again:
> <CFPARAM name="attributes.fieldName" type="string"
> default="[not provided]">
> <CFPARAM name="attributes.fieldType" type="string"
> default="string">
> <CFPARAM name="attributes.fieldValue" type="string"
> default="">
> <CFPARAM name="attributes.min" type="numeric" default="1">
> <CFPARAM name="attributes.max" type="numeric" default="1">
> <CFPARAM name="attributes.required" type="boolean"
> default="No">
> <CFPARAM name="attributes.equalValue" type="string"
> default="">
> <CFPARAM name="attributes.equalFieldName" type="string"
> default="[not
> provided]">
> <CFPARAM name="attributes.ReturnVariable"
> type="variableName"
> default="errorMessage">
> <CFSET errorMessage = "">
> <CFIF (attributes.required eq "Yes") and
> (len(attributes.fieldValue) eq 0)>
> <CFSET errorMessage = " #attributes.fieldName# is
> required.">
> <CFELSEIF (attributes.required eq "No") and
> (len(attributes.fieldValue) eq
> 0)>
> <!--- Trap to make sure we don't do a false error --->
> <CFELSE>
> <CFSWITCH expression="#attributes.fieldType#">
> <CFCASE value="string">
> <CFIF len(attributes.fieldValue) lt attributes.min>
> <CFSET errorMessage = " #attributes.fieldName# is less
> than the minimum
> #attributes.min# characters.">
> <CFELSEIF len(attributes.fieldValue) gt attributes.max>
> <CFSET errorMessage = " #attributes.fieldName# is more
> than the maximum
> #attributes.max# characters.">
> </CFIF>
> </CFCASE>
> <CFCASE value="match">
> <CFIF attributes.fieldValue neq attributes.equalValue>
> <CFSET errorMessage = " #attributes.fieldName# does
> not match
> #attributes.equalFieldName#.">
> </CFIF>
> </CFCASE>
> <CFCASE value="integer">
> <CFIF NOT isNumeric(attributes.fieldValue)>
> <CFSET errorMessage = " #attributes.fieldName# is not
> a number.">
> <CFELSEIF Val(attributes.fieldValue) lt
> Val(attributes.min)>
> <CFSET errorMessage = " #attributes.fieldName# is less
> than minimum
> #attributes.min#.">
> <CFELSEIF Val(attributes.fieldValue) gt
> Val(attributes.max)>
> <CFSET errorMessage = " #attributes.fieldName# is more
> than maximum
> #attributes.max#.">
> <CFELSEIF Val(attributes.fieldValue) neq
> Int(Val(attributes.fieldValue))>
> <CFSET errorMessage = " #attributes.fieldName# is not
> an integer.">
> </CFIF>
> </CFCASE>
> <CFCASE value="numeric">
> <CFIF NOT isNumeric(attributes.fieldValue)>
> <CFSET errorMessage = " #attributes.fieldName# is not
> a number.">
> <CFELSEIF Val(attributes.fieldValue) lt
> Val(attributes.min)>
> <CFSET errorMessage = " #attributes.fieldName# is less
> than the minimum
> #attributes.min#.">
> <CFELSEIF Val(attributes.fieldValue) gt
> Val(attributes.max)>
> <CFSET errorMessage = " #attributes.fieldName# is more
> than the maximum
> #attributes.max#.">
> </CFIF>
> </CFCASE>
> <CFCASE value="password">
> <CFIF len(attributes.fieldValue) lt 6>
> <CFSET errorMessage = " #attributes.fieldName# cannot
> be less than 6
> characters.">
> <CFELSEIF len(attributes.fieldValue) gt 50>
> <CFSET errorMessage = " #attributes.fieldName# cannot
> be more than 50
> characters.">
> <CFELSEIF REFindNoCase("[[:alpha:]]",
> attributes.fieldValue) lt 1>
> <CFSET errorMessage = " #attributes.fieldName# must
> contain at least one
> alpha character.">
> <CFELSEIF (REFindNoCase("[[:digit:]]",
> attributes.fieldValue) lt 1) and
> (REFindNoCase("[[:punct:]]",
>attributes.fieldvalue)
> lt 1)>
> <CFSET errorMessage = " #attributes.fieldName# must
> contain at least one
> numeric or punctuation character.">
> </CFIF>
> </CFCASE>
> <CFCASE value="email">
> <cfset structEmail =
> REFindNoCase("([A-Z0-9-]{1,}\.)*([A-Z0-9-]{1,})(@)(([A-Z0-
> 9-]{1,})(\.))+([A-
> Z]{2,8})", attributes.fieldValue, 1, "TRUE") >
> <CFIF ((len(attributes.fieldValue) gt 0) and
> ((structEmail.len[1] lt
> len(attributes.fieldValue)) or (structEmail.pos[1] gt 1)))
> >
> <CFSET errorMessage = " #attributes.fieldName# is not
> a valid email
> address.">
> </CFIF>
> </CFCASE>
> <CFCASE value="website">
> <cfset structWebsite =
> REFindNoCase("(([A-Z0-9-]{1,})\.)+([A-Z]{2,8})",
> attributes.fieldValue, 1, "TRUE") >
> <CFIF ((len(attributes.fieldValue) gt 0) and
> ((structWebsite.len[1] lt
> len(attributes.fieldValue)) or (structWebsite.pos[1] gt
> 1))) >
> <CFSET errorMessage = " #attributes.fieldName# is not
> a valid website
> address, example www.greatalbum.net.">
> </CFIF>
> </CFCASE>
> </CFSWITCH>
> </CFIF>
> <CFSet "Caller.#attributes.ReturnVariable#" =
> errorMessage>
> -----Original Message-----
> From: Erik Britt-Webb [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, November 26, 2002 2:12 AM
> To: CF-Talk
> Subject: Form Field validation tag -- need help (EBW)
> I've developed a form field validation tag that seems to
> work reasonably
> well, but I'm thinking it would work even better as a UDF.
> Does anyone
> agree or disagree with that conclusion? I'm attaching the
> code from the
> custom tag below because I don't really know how to
> structure the CFScript
> to accomplish the same goal as CFML. If someone could
> help me get started
> with the CFScript, I would greatly appreciate it.
> Comments on the overall
> approach are also welcome. Thanks - Erik
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription:
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Get the mailserver that powers this list at http://www.coolfusion.com