Sorry for the naff (and I do mean naff example) but I'm heading out for dinner in about 10 mins so had to dash this off. It's not elegant and would work a lot better is you passed a validation object back instead and just check for isValid etc
Here's the input form:
<cfscript>
if(StructKeyExists(request, 'Form')) Form = Request.Form;
</cfscript>
<cfparam name="Form.txtFirstName" default="" />
<cfparam name="Form.txtLastName" default="" />
<cfparam name="Form.txtAge" default="" />
<cfparam name="Form.Errors" default="StructNew()" />
<html>
<head>
<title>Person Details</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form action="" method="post" name="form1" target="_self">
<table width="75%" border="1">
<tr>
<td>First Name:</td>
<td><input name="txtFirstName" type="text" id="txtFirstName" value="<cfoutput>#Form.txtFirstName#</cfoutput>" size="40" maxlength="50"></td>
<td>
<cfif StructCount(Form.Errors) gt 0 and StructKeyExists(Form.Errors, 'txtFirstName')><cfoutput>#Form.Errors['txtFirstName']#</cfoutput></cfif>
</td>
</tr>
<tr>
<td>Last Name:</td>
<td><input
name="txtLastName" type="text" id="txtLastName" value="<cfoutput>#Form.txtLastName#</cfoutput>" size="40" maxlength="50"></td>
<td>
<cfif StructCount(Form.Errors) gt 0 and StructKeyExists(Form.Errors, 'txtLastName')><cfoutput>#Form.Errors['txtLastName']#</cfoutput></cfif>
</td>
</tr>
<tr>
<td>Age:</td>
<td><input name="txtAge" type="text" id="txtAge" value="<cfoutput>#Form.txtAge#</cfoutput>" size="40" maxlength="50"></td>
<td>
<cfif StructCount(Form.Errors) gt 0 and StructKeyExists(Form.Errors,
'txtAge')><cfoutput>#Form.Errors['txtAge']#</cfoutput></cfif>
</td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
Here is the validation form:
<cfscript>
Errors = StructNew();
if(Len(Form.txtFirstName) lt 1)
StructInsert(Errors, 'txtFirstName', 'You must enter your first name');
if(Len(Form.txtLastName) lt 1)
StructInsert(Errors, 'txtLastName', 'You must enter your last name');
if(not isNumeric(Form.txtAge) or Form.txtAge lt 1 or Form.txtAge gt 100)
StructInsert(Errors, 'txtAge', 'You must enter a valid number between 1 and 100');
if(StructCount(Errors) gt 0)
Form.Errors = Errors;
</cfscript>
<cfif StructKeyExists(Form, 'Errors') >
<cfscript>
Request.Form = Form;
GetPageContext().Forward('dsp.form.cfm');
</cfscript>
<cfelse>
<cfoutput>Congratulation #Form.txtFirstName# #Form.txtLastName#, you look great for a #Form.txtAge# year old!</cfoutput>
</cfif>
Cheers, Pete (aka lad4bear)
You are subscribed to cfcdev. To unsubscribe, send an email to [email protected] with the words 'unsubscribe cfcdev' as the subject of the email.
CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com).
CFCDev is supported by New Atlanta, makers of BlueDragon
http://www.newatlanta.com/products/bluedragon/index.cfm
An archive of the CFCDev list is available at www.mail-archive.com/[email protected]
