Solved

JavaScript radio button validation

Posted on 2013-06-01
3
452 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now