Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JavaScript radio button validation

Posted on 2013-06-01
3
Medium Priority
?
485 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 400 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 1600 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

661 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