Log in

View Full Version : Please help: javascript to validate checkbox and textbox before submitting form



alohatrojan
10-29-2007, 06:57 PM
Hi, I am new to this forum and hoping someone here can help me...thank you in advance.

I am making an enrollment form for a summer camp. At the end of the form, I would like the user to be required to: 1) enter his/her name in a text field, and 2) check a checkbox indicating they have accepted the terms and conditoins of camp enrollment, otherwise they should not be able to submit the form. For example:

(CHECKBOX HERE) "By checking this box and submitting this form, I, (USER-ENTERED NAME AS A REQUIRED FIELD), agree with terms..."

I know there is an "Accept terms and conditions script on Dynamic drive, but it does not include a required text field for the person to enter their name, only the checkbox.

Does anyone have a javascript that can do this for me? I have been stewing and throwing my hands up in the air, completely lost so I'm hoping for help.

Thank you again!

TimFA
10-30-2007, 03:48 PM
I can make something similar, but not exactly the same.

Header code:


<script language="JavaScript">
function setVisibility(id, visibility) {
document.all[id].style.display = visibility;
}
</script>


Name area & buttons:


<textarea>Your terms...</textarea><br>
<br>
Your Name: <input name="name" size="25" maxlength="255" type="text"><br>
<br>
I agree to the terms. <input onclick="setVisibility('submitbutton', 'inline')"
name="termsagree" value="agree" type="radio"><br>
<br>
I do not agree to the terms. <input checked="checked" onclick="setVisibility('submitbutton', 'none')"
name="termsagree" value="donotagree" type="radio" c><br>
<br>
<input id="submitbutton" style="display: none;" type="submit">


Add that to your existing form, no need to put your terms in that box, it can be deleted. Thats as good as I can work up, and it does same thing, simpler but the name box is not required, its just first, so most people would automatically do it first. I know your going to re-order it, I don't think that script will help to much. But its worth a shot. :D

alohatrojan
10-30-2007, 04:22 PM
Thanks Tim, this should work just fine!