We help IT Professionals succeed at work.

JavaScript radiobutton validation

medva
medva asked
on
I have a page that has multiple questions as in:

The capital of Spain is Madrid. YES | NO | I don't know
The capital of Britain is London YES | NO | I don't know

with radiobuttons. I need a javascript that would validate that the user chose one option for each question. Nothing is pre-selected when user opens the page.

Thanks.
Comment
Watch Question

Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
1. Not good practise not to select something - the user cannot find a way to deselect all - less surprising if don't know is selected.
2. what are the names for the radios?

count = 0;
for (i=0;i<myRadios.length;i++) {
   for (j=0;j<document.myForm.elements[nameOfRadio[i]].length) {
      if (document.myForm.elements[nameOfRadio[i]].checked) {
         count++;
      }
   }
}
if (count<2) {
   alert('No good')
   return false
}
IT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
assuming

nameOfRadio=new Array('r1','r2')

and

if (count<2) {

should be

if (count<nameOfRadio.length) {

to handle more radios
CERTIFIED EXPERT
Top Expert 2005

Commented:
for (Count=0; Count <4; Count ++)
     {
      if(document.form.radioButton[Count].checked)
          {
          return true;
          }
      else
          {
          alert("Please Select Yes, No, or I don't know!")
          return false;
          }
      }  

Fritz the Blank
CERTIFIED EXPERT
Top Expert 2005

Commented:
Oops, Michel beat me to it!

One addition, though, you can generalize as follows:


function checkRadio(strFieldName)

for (Count=0; Count <4; Count ++)
    {
     if(eval("document.form." + strFieldName+ "[" + Count + "].checked"))
         {
         return true;
         }
     else
         {
         alert("Please Select Yes, No, or I don't know!")
         eval("document.form." + strFieldName + ".focus()")
         return false;
         }
     }  



Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
Fritz: Did you notice that my script looks at all radios in the array given?

Michel
CERTIFIED EXPERT
Top Expert 2005

Commented:
Michel,

So,

You see what happens when you try to answer too quickly?

BTW, thank you for the sample code demonstrating the use of "this" when passing parameters.

Fritz the Blank
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
"You see what happens when you try to answer too quickly?"

???

I do not understand. I said the script I posted will validate any radio in the form if you put the name in the array...

Michel
CERTIFIED EXPERT
Top Expert 2005

Commented:
Sorry Michel,

That's not what I meant. I posted too quickly without reloading the question to see that you already answered it. I was talking about "you" in  the general sense, as in "one" in this case me.

Fritz the Blank
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
Ah - sorry I misunderstood ;-)
CERTIFIED EXPERT
Top Expert 2005

Commented:
medva,

Did any of these comments help? If so, please select one as an answer.

Fritz the Blank
CERTIFIED EXPERT
Top Expert 2005

Commented:
medva,

Did any of these comments help? If so, please select one as an answer.

Fritz the Blank
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101, Netminder or Mindphaser will return to finalize these if they are still open in 7 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20133600.html
http://www.experts-exchange.com/questions/Q.20158308.html
http://www.experts-exchange.com/questions/Q.20169923.html
http://www.experts-exchange.com/questions/Q.20188914.html
http://www.experts-exchange.com/questions/Q.20298450.html



*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
CERTIFIED EXPERT
Top Expert 2005

Commented:
Michel Plungian on this one.

Fritz the Blank
Thanks, Fritz the Blank, and all.  Sorry for the delay, too many TO DOs and not enough Moderators to do them.
:) Moondancer - EE Moderator

Explore More ContentExplore courses, solutions, and other research materials related to this topic.