[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 507
  • Last Modified:

using cfinput to validate form input

i'm having trouble using cfinput to validate form input and i'm hoping someone here with a keen eye can help me troubleshoot the javascript error.

<cfform action="updatecomponent.cfm" method="POST" preservedata="Yes">
            <table>
<tr>
                  <td>Number of Parts Required: </td>
                  <td>
                  <!--- display value from db or the updated value from user input --->            
                  <cfif IsDefined("Form.updateComponent")><cfset numParts=#Form.numPartsRequired#>
                  <cfelse><cfset numParts=#showComponents.numPartsRequired#>
                  </cfif>
                  <!--- form validation, javascript generated by coldfusion --->
                  <cfinput name="numPartsRequired" type="text" required="Yes" range="0,10" validate="integer"
                  maxlength="2" size="4" message="Number of Parts Required must be an integer between 0 and 10"
                  value="#numParts#">
                  </td>
            </tr>
<tr>
                  <td>
      <input name="componentNo" type="hidden" value="<cfoutput>#Form.componentNo#</cfoutput>">
                <input name="updateComponent" type="submit" value="Update Component">&nbsp;
                <input name="reset" type="reset" value="Reset">
            </td>
            </tr>
</table>
            </cfform>


the form basically is not validating and i'm getting a javascript error is at this line, which says value is not defined:

if  (!_CF_hasValue(_CF_this.numPartsRequired, "TEXT" ))

what's wrong with my code?

thanks!
0
etherealz
Asked:
etherealz
  • 7
  • 4
  • 2
1 Solution
 
cyberdevil67Commented:
Hi etherealz,

 It would be best to wrap your cfform with a cfoutput as I noticed you did this

<input name="componentNo" type="hidden" value="<cfoutput>#Form.componentNo#</cfoutput>">

but not here

<cfinput name="numPartsRequired" type="text" required="Yes" range="0,10" validate="integer"
               maxlength="2" size="4" message="Number of Parts Required must be an integer between 0 and 10"
               value="#numParts#">


Cheers!
0
 
etherealzAuthor Commented:
i try to put the cfoutput tag around like this:

<cfinput name="numPartsRequired" type="text" required="Yes" range="0,10" validate="integer"
                  maxlength="2" size="4" message="Number of Parts Required must be an integer between 0 and 10"
                  value="<cfoutput>#numParts#</cfoutput>">

the result is the textbox display, with <cfoutput> tags .. and didnt fix the problem
0
 
trailblazzyr55Commented:
try this..

<cfform action="updatecomponent.cfm" method="POST" preservedata="Yes">
<cfoutput>
          <table>
<tr>
               <td>Number of Parts Required: </td>
               <td>
               <!--- display value from db or the updated value from user input --->          
               <cfif IsDefined("Form.updateComponent")><cfset numParts=#Form.numPartsRequired#>
               <cfelse><cfset numParts=#showComponents.numPartsRequired#>
               </cfif>
               <!--- form validation, javascript generated by coldfusion --->
               <cfinput name="numPartsRequired" type="text" required="Yes" range="0,10" validate="integer"
               maxlength="2" size="4" message="Number of Parts Required must be an integer between 0 and 10"
               value="#numParts#">
               </td>
          </tr>
<tr>
               <td>
     <input name="componentNo" type="hidden" value="#Form.componentNo#">
                <input name="updateComponent" type="submit" value="Update Component">&nbsp;
                <input name="reset" type="reset" value="Reset">
            </td>
          </tr>
</table>
</cfoutput>
          </cfform>
0
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

 
cyberdevil67Commented:
You need validate="integer" on the numParts line.
0
 
trailblazzyr55Commented:
cyberdevil67,

I think he already had that in there, here's the input he started with...

>               <cfinput name="numPartsRequired" type="text" required="Yes" >range="0,10" validate="integer"
                              ^
                            here

i think the idea you had, just surround the form or table with <cfoutput> would work, he tried, puting the <cfouput inside the <input attrib's> and still threw an error.. moving outside like you suggested may do the trick :o)

~trail
0
 
cyberdevil67Commented:
Yeah thats what I told him, nice of him to not listen though...

And yeah I missed the validate, whoops
0
 
etherealzAuthor Commented:
i moved the cfoutput tag outside surrounding the <tr> tags and it doesnt work either.
0
 
cyberdevil67Commented:
any chance of a link to the webpage? I might suspect that you might need to trim spaces

#Trim(numParts)#

Yet if we saw the page we might be able to help more.
0
 
trailblazzyr55Commented:
this is what is wrong I think, your form form.componentNo is undefined, you need to param it, try adding

<cfparam name="form.componentNo" default="08">  of any default you want...

I tested this and had no problems :o)

~trail
0
 
trailblazzyr55Commented:
it also would help solve problems if you paramed this as well, actually this I think is where you're having issues..

add this as well...

<cfparam name="form.numPartsRequired" default="">
0
 
trailblazzyr55Commented:
here is the form I tested and seems to work fine, I don't have your query so you may need to re-arrange somethings but here's the idea..

<cfform action="updatecomponent.cfm" method="POST" preservedata="Yes">
<cfoutput>
<table>
<tr>
<td>Number of Parts Required: </td>
<td>
<!--- display value from db or the updated value from user input --->  
   
<cfparam name="form.componentNo" default="06">  
<cfif IsDefined("Form.updateComponent")>
<cfset numParts=#Form.numPartsRequired#>
   <cfelse>
            <cfif isdefined("showComponents.numPartsRequired")>
                  <cfset numParts=#showComponents.numPartsRequired#>
                  <cfelse>
                  <cfparam name="showComponents.numPartsRequired" default="00">
                  <cfset numParts=#showComponents.numPartsRequired#>
            </cfif>
</cfif>
<!--- form validation, javascript generated by coldfusion --->
<cfinput name="numPartsRequired"
         type="text"
             required="Yes"
             range="0,10"
             validate="integer"
             maxlength="2"
             size="4"
             message="Number of Parts Required must be an integer between 0 and 10"
         value="#numParts#">
</td>
</tr>
<tr>
<td>
<input name="componentNo" type="hidden" value="#Form.componentNo#">
<input name="updateComponent" type="submit" value="Update Component">&nbsp;
<input name="reset" type="reset" value="Reset">
</td>
</tr>
</table>
</cfoutput>
</cfform>
0
 
trailblazzyr55Commented:
waiting to hear back from etherealz ;o)
0
 
trailblazzyr55Commented:
hey mrichmon,

sorry for the vague post on my last post, just catching up to them ;o)

the example I posted on 05/09/2005 11:39AM PDT worked for me and should work for etherealz, unless there's still some error? what I put there should've solved the question though as I got an example of etherealz's code working where before it wasn't. What I posted on 05/09/2005 11:32AM PDT I believe was the issue, once adding that, it ran without problems.. taking care of the undefined issue etherealz originally had...

regards,
~trail
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 7
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now