Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to validate a check box before a link can be clicked? 1

Status
Not open for further replies.

survivorjoe

Technical User
Dec 6, 2000
26
0
0
US
Greetings,

I have the code shown below. When I click on the "Get Application" link, I want it to work only when the checkbox is checked. If it is not checked, I want a pop-up alert box to appear. I have tried a few things, but I am not getting it right. Here is the html code. What javascript is needed to make this concept work? Many thanks!
-----------------------------------------------------
<tr>
<td width=&quot;336&quot;><div class=&quot;download&quot;><a href=&quot;app.pdf&quot; target=&quot;_new&quot;>Get Application</a></div>
<div class=&quot;download&quot;>Info about the app. Blah, blah, blah.</div><p>
<table><tr><td valign=&quot;top&quot;><form name=&quot;vendor_app&quot;><b>*</b><input type=&quot;checkbox&quot; name=&quot;vendor_app&quot;></form></td><td valign=&quot;top&quot;><div class=&quot;download&quot;>I agree to the terms of this program, blah, blah, blah.</div></td></tr></table>
</td>
</tr>
 
You should name the form and the checkbox with different names.
Try:
<a href=&quot;app.pdf&quot; target=&quot;_new&quot; onclick=&quot;return document.formName.checkboxName.checked&quot;>Get Application</a>

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life=life-1};
 
shouldn't name the form and the box the same...

<a href=&quot;app.pdf&quot; target=&quot;_new&quot; onClick=&quot;return document.vendor_app.vendor_box.checked&quot;>Get Application</a>

<form name=&quot;vendor_app&quot;><b>*</b><input type=&quot;checkbox&quot; name=&quot;vendor_box&quot;></form>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
This is more elegant...

<a href=&quot;app.pdf&quot; target=&quot;_new&quot; onClick=&quot;return confirmIt()&quot;>Get Application</a>

<form name=&quot;vendor_app&quot;><b>*</b><input type=&quot;checkbox&quot; name=&quot;vendor_box&quot;></form>

<script>
function confirmIt(){
if (document.vendor_app.vendor_box.checked){
return true;
}
else{
alert(&quot;Please check the box&quot;)
return false;
}
}
</script>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
OK, yes that works. Thanks mwolf00. Now, if I have two different sections, each with its own checkbox, how do I pass a parameter to the code so I don't have to make 2 functions?

- Joe
 
Hey mwolf00, I think I am OK with using two separate functions as I have two different messages to display. Does your solution work on the Netscape 4.7 series of browsers? Thanks again for your help.

- Joe
 
Joe - I don't do much work with Netscape, so you'll have to test it on your system. I believe that it should work fine.

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top