Skip to main content
Inspiring
April 28, 2009
Question

How do I validate this ?

  • April 28, 2009
  • 2 replies
  • 919 views

I use the following code to dynamically generate input fields based on the quantity :

<cfloop from="1" to="#quantity#" index="i">
<table border="0">
<tr>
<td width="33" align="right" class="TitleText"><b>#i#:</b> </td>
<td width="148" align="right" clsas="TitleText">
<cfinput type="text" name="serialNumber" size="40">
<input type="hidden" name="PartNumberID" value="#PartNumberID#"><br></td>
</tr>
</table>
</cfloop>

The problem I am having is that people purposely blowup the serialNumber field by exceeding the limit size 40. I can use maxlenght to prevent this, but I want to display an error message. How can I do this ?

    This topic has been closed for replies.

    2 replies

    April 28, 2009

    Like this...

    <cfif isDefined("form.serialNumber")>

         <cfif len(trim(form.serialNumber)) gt 40>

              <p>Your input is too long</p>

         </cfif>

    </cfif>

    However, as the previous post pointed out - you need to give each of those input boxes a unique name, you can do this like so:

    <cfoutput>

    <cfloop from="1" to="#quantity#" index="i">

    <cfinput type="text" name="serialNumber_#i#" size="40">

    </cfloop>

    </cfoutput>

    The index of the loop becomes appended to each fields name and therefore makes it distinguishable from the rest.

    You might also want to use jQuery or some other kind of JavaScript to validate it all before it gets to the server. But you should probably always use both.

    Cheers,

    Mikey.

    trojnfnAuthor
    Inspiring
    April 28, 2009

    ok, I understand giving it a unique name. But your edit example is on the server side and does not contain the unique name ?

    Inspiring
    April 28, 2009

    For server side,

    <cfloop list="#form.fieldnames#" index="ThisField">

    <cfif left(ThisField, 12) is "SerialNumber">

    now you can do len(form[ThisField]) and decide what you want to do with the results.  There might be more than one that's longer than 40 characters.

    Inspiring
    April 28, 2009

    If you want to do it on the client, you are going to have to have different names for each text box.  You can use your loop index for that.