Forum Moderators: open

Message Too Old, No Replies

Form validation

Force 'yes' answers to a form using dropdown menu

         

ed_edin

11:55 am on May 28, 2004 (gmt 0)

10+ Year Member



Hi

This continues an old thread which I can't reply to anymore: "A form with 5 questions. Each question must be answered with a "yes" answer. Is it possible to use java script to validate that all answers are yes in order for the form to be submitted?". I've included the relevant parts and my question is at the end.

korkus2000 recommended this script:

<script language="JavaScript">
function checkSelects() {
//set a variable to hold the string info
var selectedNo = "";
//test the select boxes for their values
if (document.nameOfForm.NameOfDropDown1.options[document.nameOfForm.NameOfDropDown1.selectedIndex].value!= "yes") {
//If they are not equal to yes then add the text for the alert box of which
//select box is not equal to yes.
selectedNo += "\n - People";
}
if(document.nameOfForm.NameOfDropDown2.options[document.nameOfForm.NameOfDropDown2.selectedIndex].value!= "yes") {
selectedNo += "\n - Places";
}
if (document.nameOfForm.NameOfDropDown3.options[document.nameOfForm.NameOfDropDown3.selectedIndex].value!= "yes") {
selectedNo += "\n - Things";
}
//test to see if you have any string info in the variable
if (selectedNo!= "") {
//If so write an alert box telling which hasn't been selected to yes
selectedNo ="_____________________________\n" +
"You select no for:\n" +
selectedNo + "\n_____________________________" +
//Explain why you don't want these people submiting
"\nWe don't like negative people!\nSo we don't want you to submit\nyour form!";
alert(selectedNo);
//Stop the submit from going through
return false;
}
//No "no"s so let them submit.
else return true;
}
</script>

and the form:

<form name="nameOfForm" onSubmit="checkSelects()">
Do You Like People?
<select name="NameOfDropDown1">
<option value="no">No</option>
<option value="yes">Yes</option>
</select><br>
Do you like Places?
<select name="NameOfDropDown2">
<option value="no">No</option>
<option value="yes">Yes</option>
</select><br>
Do You Like Things?
<select name="NameOfDropDown3">
<option value="no">No</option>
<option value="yes">Yes</option>
</select>
<br>
<input type="submit">
</form>

This still redirects whether people select all 'yes' or not, so Dr. DOc recommended:

Then don't use a submit button.
Use a regular input button instead:

<input type="button" value="Submit" onclick="checkSelects()">

At the end of your JavaScript function (where you end up if they are all "yes") put this:

document.name_of_form.submit();
Then don't use a submit button.
Use a regular input button instead:

<input type="button" value="Submit" onclick="checkSelects()">

At the end of your JavaScript function (where you end up if they are all "yes") put this:

document.name_of_form.submit();

-------
My question is,

I'm not sure where
document.name_of_form.submit();
should go, where is the end of the Javascript function?

Many thanks,

Ed

korkus2000

12:07 pm on May 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you link to the original thread so I can remember what I was doing?

ed_edin

12:31 pm on May 28, 2004 (gmt 0)

10+ Year Member



Sure, it's:

[webmasterworld.com...]

I reached it by typing 'validate radio buttons' in the site search.

Ed