checkbox validation javascript

Hello,

I can't get it to work. it should alert when non of the 3 boxes are checked.
If 1 is checked it's ok


<html>
<head>
<SCRIPT>
function validate()
{
if (form1.answer.checked=='false') {
alert('Please answer.');
event.returnValue=false;
}
</script>
</head>
<body>
<form name="form1" method="post" action="" onSubmit="validate()">
  <p>
    <input name="answer" type="checkbox" id="answer" value="yes">
    yes
    <input name="answer" type="checkbox" id="answer" value="no">
    no
    <input name="answer" type="checkbox" id="answer" value="maybe">
    maybe
</p>
  <p>
    <input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>
mvanthofAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BatalfCommented:
<html>
<head>
<SCRIPT>
function validate()
{
    var checked=false;
    var formObj = document.form1;
    for(var no=0;no<formObj.answer.length;no++){
        if(formObj.answer[no].checked)return true;
   
    }
    if(!checked){
        alert('Please answer.');
        return false;
    }
    return true;
}
</script>
</head>
<body>
<form name="form1" method="post" action="" onSubmit="validate()">
  <p>
    <input name="answer" type="checkbox" id="answer" value="yes">
    yes
    <input name="answer" type="checkbox" id="answer" value="no">
    no
    <input name="answer" type="checkbox" id="answer" value="maybe">
    maybe
</p>
  <p>
    <input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>
0
sajuksCommented:
//try this
<HTML>
<HEAD>
<TITLE></TITLE>
<script>
     function validate()
     {
          elements = document.getElementsByTagName("input");
          counter = 0;
          for (i =0; i < elements.length; i++)
          {
               if(elements[i].type == "checkbox" && elements[i].checked)
               {
                    counter++;
               }
          }
          if(counter == 0)
          {
               alert("Nothing selected");
                  return false;
          }
          else
          {
                alert("OK");
                return true;
          }

     }
</script>
</HEAD>
<BODY>
<table>
     <tr>
          <td>
               <form name="f1" id="f1" action="" onSubmit="return validate();">
                    <input type="checkbox" name="answer" value="yes">
                    <input type="checkbox" name="answer" value="no">
                    <input type="checkbox" name="answer" value="maybe">
                    <br>
                    <input type="submit" value="Check" >
               </form>
          </td>
     </tr>
</table>
</BODY>
</HTML>
0
sajuksCommented:
hey batalf guess u forgot to put the return statement in ur form submit ..

<form name="form1" method="post" action="www.google.com" onSubmit="return validate()">
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Michel PlungjanIT ExpertCommented:
Are you sure you want to use checkboxes for that?
Are the answers mutually exclusive then you should use radios

<form name="form1" method="post" action="" onSubmit="validate()">
  <p>
    <input name="answer" type="radio" id="answer" value="yes">
    yes
    <input name="answer" type="radio" id="answer" value="no">
    no
    <input name="answer" type="radio" id="answer" value="maybe" CHECKED>
    maybe
</p>
  <p>
    <input type="submit" name="Submit" value="Submit">
</p>
</form>
0
mvanthofAuthor Commented:
ok... what if I make them all radio... how will the validation script look like...?
0
BatalfCommented:
Just change checkbox to radio

<html>
<head>
<SCRIPT>
function validate()
{
    var checked=false;
    var formObj = document.form1;
    for(var no=0;no<formObj.answer.length;no++){
        if(formObj.answer[no].checked)return true;
   
    }
    if(!checked){
        alert('Please answer.');
        return false;
    }
    return true;
}
</script>
</head>
<body>
<form name="form1" method="post" action="" onSubmit="return validate()">
  <p>
    <input name="answer" type="radio" id="answer" value="yes">
    yes
    <input name="answer" type="radio" id="answer" value="no">
    no
    <input name="answer" type="radio" id="answer" value="maybe">
    maybe
</p>
  <p>
    <input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mvanthofAuthor Commented:
and if I have some more options? like 3 radiobuttons for answer and 3 for age

<p>
    <input name="age" type="radio" id="answer" value="10-20">
    yes
    <input name="age" type="radio" id="answer" value="21-40">
    no
    <input name="age" type="radio" id="answer" value="41-60">
    maybe
</p>
0
Michel PlungjanIT ExpertCommented:
Delete the script and just do


<p>
    <input name="age" type="radio" id="answer" value="No answer" CHECKED>No answer
    <input name="age" type="radio" id="answer" value="10-20">10-20
    <input name="age" type="radio" id="answer" value="21-40">21-40
    <input name="age" type="radio" id="answer" value="41-60">41-60
</p>

and if no answer is not allowed, just test
document.forms.answer[0].checked
0
mvanthofAuthor Commented:
???????

<html>
<head>
<SCRIPT>
function validate()
{
    var checked=false;
    var formObj = document.form1;
    for(var no=0;no<formObj.answer.length;no++){
        if(formObj.answer[no].checked)return true;
   
    }
    if(!checked){
        alert('Please answer.');
        return false;
    }
    return true;
}
</script>
</head>
<body>
<form name="form1" method="post" action="" onSubmit="return validate()">
  <p>
    <input name="answer" type="radio" id="answer" value="yes">
    yes
    <input name="answer" type="radio" id="answer" value="no">
    no
    <input name="answer" type="radio" id="answer" value="maybe">
    maybe
</p>
<p>
    <input name="age" type="radio" id="answer" value="10-20">
    10-20
    <input name="age" type="radio" id="answer" value="21-40">
    21-40
    <input name="age" type="radio" id="answer" value="41-60">
    41-60
</p>

  <p>
    <input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>
0
BatalfCommented:
Go for mplungjans solution or modify the script above by

1) Numbering your answer radios answer1, answer2 etc.
2) Make your function loop through the radio buttons.

New code with these changes:

<html>
<head>
<SCRIPT>
function validate()
{
   
    var formObj = document.form1;
    currentIndex=1;
    while(formObj.elements['answer'+currentIndex]){
          var checked=false;
          for(var no=0;no<formObj.elements['answer'+currentIndex].length;no++){
              if(formObj.elements['answer'+currentIndex][no].checked)checked=true;
          
          }          
          if(!checked){
              alert('Please answer all questions ');
              return false;
          }  
          currentIndex++;
   
    }


    return true;
}
</script>
</head>
<body>
<form name="form1" method="post" action="" onSubmit="return validate()">
  <p>
    <input name="answer" type="radio" id="answer1" value="yes">
    yes
    <input name="answer" type="radio" id="answer1" value="no">
    no
    <input name="answer" type="radio" id="answer1" value="maybe">
    maybe
</p>
<p>
    <input name="age" type="radio" id="answer2" value="10-20">
    10-20
    <input name="age" type="radio" id="answer2" value="21-40">
    21-40
    <input name="age" type="radio" id="answer2" value="41-60">
    41-60
</p>

  <p>
    <input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.