We help IT Professionals succeed at work.

cfinput validation with text and checkbox with JS (passing values & disable)

343 Views
Last Modified: 2013-12-24
I have a text box and a check box. Right now I have the code working for when teh checkbox is click the textbox is disabled. But I want the the checkbox to give the text box a value of -888 if the checkbox is CLICKED. I do not want the value to SHOW UP IN THE TEXT BOX! I just want to pass it to the action page to insert -888 in the database if they do not know.  The big thing is I want to keep my CFinput validation!
Can I do this still? Right now the textbox is not getting the value because I'm clearing it out. I need th action page to get that value but it's reading no response it should say don't know. HELP!

<cfif recinj is -888><cfset DK=""><cfelse><cfset DK=recinj></cfif>

<cfinput  type="text" name=recinj  validate="float" validateat="onsubmit" message="Please enter numbers only, in Total Recordables" size="15" maxlength="20" value="#DK#"> Total Recordables <br>

<INPUT name="recinj2" type=checkbox Value="-888" onClick="setValue(this.name,'recinj')" <cfif recinj is -888>checked</cfif>>Don't know</td>

<SCRIPT LANGUAGE=JAVASCRIPT TYPE="TEXT/JAVASCRIPT">

      function setValue(strCheckName,strTextName){
     var objFormField = document.forms[0]
     if(objFormField.elements[strCheckName].checked){
          objFormField.elements[strTextName].value= "";
          objFormField.elements[strTextName].disabled=true;
     }else{
          objFormField.elements[strTextName].value="";
          objFormField.elements[strTextName].disabled=false;
     }

}

</SCRIPT>
Comment
Watch Question

Author

Commented:
I forgot to mention that the checkbox needs to stay clicked upon revisted or the value is changed! Right now it won't stay checked after I saved
James RodgersWeb Applications Developer
CERTIFIED EXPERT

Commented:
put this in the form processing page
<cfif isDefined('form.recinj2')>
      <cfset insertValue=#form.recinj2#>
<cfelse>
      <cfset insertValue=#form.recinj#>
</cfif>

and change teh inserted value in teh query from  recinj to insertValue

Author

Commented:
OK I'll try that. FYI recinji is the only define variable in the DB

Author

Commented:
That didn't work and it still says No response in the action page.

This is what I have in that action page:
<cfif isDefined('form.recinj2')>
      <cfset insertValue=#form.recinj2#>
<cfelse>
      <cfset insertValue=#form.recinj#>
</cfif>
<cfif recinj is -888><font color="##FF0000"> <strong>Don't Know </strong></font><cfelseif recinj is ""> <font color="##FF0000"> <strong>No Response</strong> </font> <cfelse>#recinj#</cfif>
James RodgersWeb Applications Developer
CERTIFIED EXPERT

Commented:
this
<cfif isDefined('form.recinj2')>
      <cfset insertValue=#form.recinj2#>
<cfelse>
      <cfset insertValue=#form.recinj#>
</cfif>
is supposed to go in teh page that performs teh data insert

Author

Commented:
Yes I know. In the action page. I have my query insert and update on this page
James RodgersWeb Applications Developer
CERTIFIED EXPERT

Commented:
did you do a cfdumpo to see what teh form contains?

<cfdump var=#form#">
does it show
recinj2
recinj
?

Author

Commented:
It says Variable RECINJ2 is undefined. in both pages!
James RodgersWeb Applications Developer
CERTIFIED EXPERT

Commented:
so it is not listed in the dump? then there is something wrong with the form, a text input should always be defined in a form even if it has no value, unless it is disables, in which case you should se teh checkbox name in teh dump

Author

Commented:
Ok I had to change this  value="#DK#" to  value="#recinj#">

But the dump came back empty string

<cfif recinj is -888><cfset DK=""><cfelse><cfset DK=recinj></cfif>

<cfinput  type="text" name=recinj  validate="float" validateat="onsubmit" message="Please enter numbers only, in Total Recordables" size="15" maxlength="20" value="#DK#"> Total Recordables <br>

<INPUT name="recinj2" type=checkbox Value="-888" onClick="setValue(this.name,'recinj')" <cfif recinj is -888>checked</cfif>>Don't know</td>

Author

Commented:
I got values now for recinj2 (-888) but when I go back, the checkbox is not check because it's checking against the recinj and not the recinj2
James RodgersWeb Applications Developer
CERTIFIED EXPERT

Commented:
try this
<INPUT name="recinj2" type=checkbox Value="-888" onClick="setValue(this.name,'recinj')" <cfif not DK>checked</cfif>>Don't know</td>

Author

Commented:
I tried and got this: cannot convert the value "" to a boolean

I don't think I can do this  <cfif not DK>checked</cfif>>??
James RodgersWeb Applications Developer
CERTIFIED EXPERT

Commented:
>>I don't think I can do this  <cfif not DK>checked</cfif>>??
no, try this instead

<cfif not trim(DK) EQ ''>checked</cfif>>??

Author

Commented:
No error this time but no saved check
Web Applications Developer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
I changed it to and THAT SO WORKED!!!THANKS! I'm so happy!
James RodgersWeb Applications Developer
CERTIFIED EXPERT

Commented:
glad i could help

thanks for the points

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.