Form Validation - check textbox filled in if radio button clicked

M

McKirahan

Claire Osborne said:
I have created a form at
http://www.habitatforhumanity.org.uk/html/gv_indiv_apply.htm

In the Medical Details section we need to ensure that if a radio
button is clicked to indicate a health issue then the textarea box is
filled in.

Is there a way to do this?

I am not an expert in Javascript so please treat me as stupid!

Thanks.


Here's a function that will identify which textareas are blank when there
corresponding radiobutton is "yes".

function radiotext() {
var form = document.application;
var errs = "";
for (i=0; i<form.elements.length; i++) {
if (form.elements.type == "radio") {
if (form.elements.value == "yes") {
if (form.elements.checked) {
if (form.elements[i+1].value == "") {
errs += "\n" + form.elements[i+1].name;
}
}
}
}
}
if (errs != "") {
alert("Please enter:\n" + errs);
return false;
}
return true;
}


It can be called by adding the following line to the "form" tag:

onsubmit="return radiotext()"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top