Compare ColdFusion form fields with validation

Hi Experts

Coldfusion newbie and I have come unstuck with form validation.

I need to specify that if one field has a certain value then this limits the valid entry’s for three other fields like so.

Field A is 01

Field B can be only 01 or 02
Field C has to be 99
Field D can be 01, 02 or 03

Any combination of valid entry’s and I would like the form data to be submitted.

I cannot seem to get this to work.

I have tried server side validation using cfif statements and cfcase statements.

I am running Coldfusion MX 7 and using a flash form.

500 points for a comprehensive answer to this one. As I cannot work it out and the web has not pointed me in the right direction.

Thanks

Clive
clivecrawdevilAsked:
Who is Participating?
 
danrosenthalConnect With a Mentor Commented:
Here is a very simplistic answer, I would add some feedback to display to the user to tell them exactly what was wrong.

<CFSET error = 0>
<CFIF form.field_a eq "01">
    <CFIF NOT LISTFIND("01,02",form.field_b)>
        <CFSET error = 1>
    </CFIF>
    <CFIF form.field_c neq "99">
        <CFSET error = 1>
    </CFIF>
    <CFIF NOT LISTFIND("01,02,03",form.field_d)>
        <CFSET error = 1>
    </CFIF>
</CFIF>

<CFIF error eq 0>
  <!--- Process Form Here --->
<CFELSE>
  Sorry, there was an validation error.
</CFIF>
0
 
mrichmonCommented:
Okay then you need to show us exactly what the restrictions are.
0
 
clivecrawdevilAuthor Commented:
Hi I tried this gonna post the entire site in order for you to be able to see the entire code.

<CFSET error = 0>
<CFIF form.E12 eq "01">
    <CFIF NOT LISTFIND("01,02",form.E13)>
        <CFSET error = 1>
    </CFIF>
    <CFIF form.E14 neq "99">
        <CFSET error = 1>
    </CFIF>
    <CFIF NOT LISTFIND("01,02,03",form.E15)>
        <CFSET error = 1>
    </CFIF>
</CFIF>
<CFIF error eq 0>
<CFSET error = 1>
<CFIF form.E12 eq "03">
    <CFIF NOT LISTFIND("01,02",form.E13)>
        <CFSET error = 2>
    </CFIF>
    <CFIF form.E14 neq "99">
        <CFSET error = 2>
    </CFIF>
    <CFIF NOT LISTFIND("02",form.E15)>
        <CFSET error = 2>
    </CFIF>
</CFIF>
<CFIF error eq 1>
<CFSET error = 2>
<CFIF form.E12 eq "02,05">
    <CFIF from.E13 neq "98">
        <CFSET error = 3>
    </CFIF>
    <CFIF form.E14 neq "99">
        <CFSET error = 3>
    </CFIF>
    <CFIF from.E15 neq "99">
        <CFSET error = 3>
    </CFIF>
</CFIF>
<CFIF error eq 2>
<CFSET error = 3>
<CFIF form.E12 eq "04">
    <CFIF NOT LISTFIND("01,02,03,04,05",form.E14)>
        <CFSET error = 4>
    </CFIF>
    <CFIF form.E13 neq "03">
        <CFSET error = 4>
    </CFIF>
    <CFIF form.E15 neq "99">
        <CFSET error = 4>
    </CFIF>
</CFIF>
<CFIF error eq 3>
<!--- Insert the new record --->
<cfinsert datasource="ILRData" tablename="tblILR" formfields="L03,L09,L10,L18,L19,L20,L21,L17,L23,L11,L13,L14,L12,L27A,L27B,L25,L01,A47B2,A23,E22,E23,A09,E12,A48A,A48B,A47A,E13A,E13B,E14,E15,A47B1,E20,E21,L35,A24,E17,E16,A10,E08,E09,E18,E19,A46,A49,L42,A34,A35,L39,E10">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>BFPan Learners</title>
<style type="text/css">
<!--
body {
      background-color: #26468F;
      background-image: url(Images/PpBg.png);
      margin-left: 100px;
      margin-right: 100px;
      margin-top: 30px;
      margin-bottom: 100px;
}
body,td,th {
      font-family: Arial, Helvetica, sans-serif;
      font-size: 14px;
      color: #FFFFFF;
}
.style1 {color: #5EB05E}
a {
      font-size: 14px;
      color: #FFFFFF;
}
a:link {
      text-decoration: none;
}
a:visited {
      text-decoration: none;
      color: #CCCCCC;
}
a:hover {
      text-decoration: underline;
      color: #5EB05E;
}
a:active {
      text-decoration: none;
}
.style2 {font-size: 18px}
-->
</style></head>

<body>
<table width="200" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><img src="Images/BlueBGTopLeft.png" width="20" height="20"></td>
    <td bgcolor="#26468F">&nbsp;</td>
    <td><img src="Images/BlueBGTopRight.png" width="20" height="20"></td>
  </tr>
  <tr>
    <td height="640" bgcolor="#26468F">&nbsp;</td>
    <td>
      <table width="818" border="0" cellpadding="5" cellspacing="5" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
  <tr align="right" valign="top" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
    <td height="191" align="left" valign="top" bordercolor="#FFFFFF"><table width="796" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="20"><img src="Images/BlueTopLeft.gif" width="20" height="20"></td>
        <td width="756" bgcolor="#26468F">&nbsp;</td>
        <td width="20"><img src="Images/BlueTopRight.gif" width="20" height="20"></td>
      </tr>
      <tr>
        <td rowspan="3" bgcolor="#26468F">&nbsp;</td>
        <td bgcolor="#26468F"><h1 align="center" class="style1">Building Foundations Pan London </h1></td>
        <td rowspan="3" bgcolor="#26468F">&nbsp;</td>
      </tr>
      <tr>
        <td><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="756" height="52">
            <param name="movie" value="SWF/menu.swf">
            <param name="quality" value="high">
            <param name="BGCOLOR" value="#26468F">
            <embed src="SWF/menu.swf" width="756" height="52" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" bgcolor="#26468F"></embed>
        </object></td>
      </tr>
      <tr>
        <td><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="756" height="52">
            <param name="movie" value="SWF/6_larger_buttons.swf">
            <param name="quality" value="high">
            <param name="BGCOLOR" value="#26468F">
            <embed src="SWF/6_larger_buttons.swf" width="756" height="52" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" bgcolor="#26468F"></embed>
        </object></td>
      </tr>
      <tr>
        <td><img src="Images/BlueBottomLeft.png" width="20" height="20"></td>
        <td bgcolor="#26468F">&nbsp;</td>
        <td><img src="Images/BlueBottomRight.gif" width="20" height="20"></td>
      </tr>
    </table></td>
  </tr>
  <tr align="right" valign="top" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
    <td width="798" height="434" align="center" valign="top" bordercolor="#FFFFFF" bgcolor="#FFFFFF"><table border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="20" height="20"><img src="Images/GreenTopLeft.png" width="20" height="20"></td>
        <td width="360" bgcolor="#75BC75">&nbsp;</td>
        <td width="385" bgcolor="#75BC75">&nbsp;</td>
        <td width="20"><img src="Images/GreenTopRight.png" width="20" height="20"></td>
      </tr>
      <tr>
        <td height="32" bgcolor="#26468F">&nbsp;</td>
        <td colspan="2" align="center" valign="top" bgcolor="#26468F"><span class="style2">
            <cfoutput> #L10# #L09# has been sucessfully added to the ILRDatabase </cfoutput> &nbsp;</span></td>
        <td bgcolor="#26468F">&nbsp;</td>
      </tr>
      <tr>
        <td height="352" bgcolor="#26468F">&nbsp;</td>
        <td colspan="2" align="center" valign="top" bgcolor="#26468F">
            <table width="200" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td><div align="center"><a href="ILRForm.cfm" target="_self"><strong>Add Learner </strong></a></div></td>
            </tr>
            <tr>
              <td align="center" valign="top"><div align="center"><span class="style3">Click the link above to add a learner </span></div></td>
            </tr>
            <tr>
              <td><div align="center"><a href="ILRSearchForm.cfm" target="_self"><strong>Edit Learner</strong></a></div></td>
            </tr>
            <tr>
              <td align="center" valign="top"><div align="center"><span class="style3">Click the link above to edit learners you have already added </span></div></td>
            </tr>
            <tr>
              <td align="center" valign="top"><div align="center"><a href="Section3SearchForm.cfm" target="_self"><strong>Complete learner</strong></a></div></td>
            </tr>
            <tr>
              <td align="center" valign="top"><div align="center"><span class="style3">Click the above link to fill in section 3 of the ILR Form and complete a learner </span></div></td>
            </tr>
            <tr>
              <td align="center" valign="top"><div align="center"><a href="ILRSearchForm.cfm" target="_self"><strong>Print ILR</strong></a></div></td>
            </tr>
            <tr>
              <td align="left" valign="top"><div align="center"><span class="style3">Click the above link to print a ILR form directly from the ILRDatabase </span></div></td>
            </tr>
            <tr>
              <td align="center" valign="top"><div align="center"><strong>Add Exam </strong></div></td>
            </tr>
            <tr>
              <td align="center" valign="top"><span class="style3">Click the above link to enter candidates exam details </span></td>
            </tr>
            <tr>
              <td align="center" valign="top"><div align="center"><strong>Learner survey </strong></div></td>
            </tr>
            <tr>
              <td align="center" valign="top"><span class="style3">Click the above link to complete the learner survey</span></td>
            </tr>
            <tr>
              <td height="16">&nbsp;</td>
            </tr>
          </table>
            </td>
        <td bgcolor="#26468F">&nbsp;</td>
      </tr>
      <tr>
        <td height="20"><img src="Images/BlueBottomLeft.png" width="20" height="20"></td>
        <td colspan="2" bgcolor="#26468F">&nbsp;</td>
        <td><img src="Images/BlueBottomRight.png" width="20" height="20"></td>
      </tr>
    </table></td>
  </tr>
</table>
</td>
    <td bgcolor="#26468F">&nbsp;</td>
  </tr>
  <tr>
    <td height="20"><img src="Images/BlueBGBottomLeft.png" width="20" height="20"></td>
    <td bgcolor="#26468F">&nbsp;</td>
    <td><img src="Images/BlueBGBottomRight.png" width="20" height="20"></td>
  </tr>
</table>
</body>
</html>
<CFELSE>
  E12 = 02 or 05 E13 needs to be 98 E14 needs to be 99 E15 needs to be 99.
</CFIF>
<CFELSE>
  E12 = 04 E13 needs to be 03 E14 needs to be 01,02,03,04,05 E15 needs to be 99.
</CFIF>
<CFELSE>
  E12 = 03 E13 needs to be 01 or 02 E14 needs to be 99 E15 needs to be 02.
</CFIF>
<CFELSE>
  E12 = 01 E13 needs to be 01 or 02 E14 needs to be 99 E15 needs to be 01,02,03.
</CFIF>

When i submit i get a String index out of range: -2 error message no idea where to start.

I Thought i would be able to nest the cfif statements in order to achive all the required validation.

The fields involved are E12, E13, E14 and E15.

the logic needs to be

 E12 = 02 or 05 E13 needs to be 98 E14 needs to be 99 E15 needs to be 99.

 E12 = 04 E13 needs to be 03 E14 needs to be 01,02,03,04,05 E15 needs to be 99.

 E12 = 03 E13 needs to be 01 or 02 E14 needs to be 99 E15 needs to be 02.

 E12 = 01 E13 needs to be 01 or 02 E14 needs to be 99 E15 needs to be 01,02,03.

Any 1 of the above is valid and the form should be submitted anything else should be thrown back with an error message.

Any help would be greatly appriciated.

Thanks

Clive



0
All Courses

From novice to tech pro — start learning today.