Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 487
  • Last Modified:

JavaScript radio button validation

I am validating some radio buttons on my form. When users forget to check a radio button group an alert window pops up to remind them. The problem is that a separate alert window pops up for each ignored question. How could I modify this script so that only one alert window pops up reminding users that each question has to be answered?
Thanks for your help.
<script type="text/javascript">

<!--

function validate_form ( )
{
	valid = true;

if ( ( document.EvaluationForm.Signature[0].checked == false ) && ( document.EvaluationForm.Signature[1].checked == false ))
{
alert ( "Did you sign the report?" );
valid = false;
}

if ( ( document.EvaluationForm.Doors[0].checked == false ) && ( document.EvaluationForm.Doors[1].checked == false ) && ( document.EvaluationForm.Doors[2].checked == false ))
{
alert ( "Was the door closed?" );
valid = false;
}

if ( ( document.EvaluationForm.Window[0].checked == false ) && ( document.EvaluationForm.Window[1].checked == false ) && ( document.EvaluationForm.Window[2].checked == false ))
{
alert ( "Was the window closed?" );
valid = false;
}

if ( ( document.EvaluationForm.Pool[0].checked == false ) && ( document.EvaluationForm.Pool[1].checked == false ) && ( document.EvaluationForm.Pool[2].checked == false ))
{
alert ( "Did you clean the Pool?" );
valid = false;
}

if ( ( document.EvaluationForm.Carrots[0].checked == false ) && ( document.EvaluationForm.Carrots[1].checked == false ) && ( document.EvaluationForm.Carrots[2].checked == false ))
{
alert ( "Do you like carrots?" );
valid = false;
}


return valid;
}

//-->

</script>




<input type="radio" name="Signature" id="Signature.0" onclick="showHide('Yes','block');showHide('No','none');"> Yes
<input type="radio" name="Signature" id="Signature.1" onclick="showHide('Yes','none');showHide('No','block');"> No


<input type="radio" name="Doors" value="Yes" id="Doors.0" alt="Yes" title="Yes" /> Yes
<input type="radio" name="Doors" value="No" id="Doors.1" alt="No" title="No" /> No
<input type="radio" name="Doors" value="NA" id="Doors.2" alt="N/A" title="NA" /> N/A

etc.

Open in new window

0
romsom
Asked:
romsom
2 Solutions
 
GaryCommented:
Declare a blank var at the start e.g. msg=""
For each of your alerts above append the var instead of an alert e.g. msg+="Your message/n"

Then at the end

if (msg!=""){
    alert(msg);
    valid=false
}
return valid;
0
 
Rainer JeschorCommented:
Hi,
something like this:
http://jsfiddle.net/EE_RainerJ/ZApXS/
HTH
Rainer
0
 
romsomAuthor Commented:
Thank you very much. RainerJ, I really appreciate your complete solution; I've just realized how cool it looks with my real form.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now