Solved

JavaScript radio button validation

Posted on 2013-06-01
3
479 Views
Last Modified: 2013-06-01
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
Comment
Question by:romsom
3 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 100 total points
ID: 39212839
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
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 400 total points
ID: 39212844
Hi,
something like this:
http://jsfiddle.net/EE_RainerJ/ZApXS/
HTH
Rainer
0
 

Author Closing Comment

by:romsom
ID: 39212848
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question