-1

I apologize, if the title is not correctly set as I have no actual clue what is causing it, but my loop will always result in "false" statement.

Straight to the point, here is the code, same as below, if anyone prefer to check it in Codepen..

function Validation() {
 if(!ValidateForm()) {
  document.getElementById("errorBox").innerHTML = "Please double check, that you have answered all the questions and try again."
  return false;
 } else {
  document.getElementById("errorBox").innerHTML = ""
  alert("Thank you for your cooperation");      
 }
}
function ValidateForm() {
 var a = document.getElementsByName('Activity');
 var v = document.getElementsByName('Volunteers');
 //var e = document.getElementsByName('Name');
       
 for (var i = 0; i<a.length; i++) {
  if (a[i].type == 'checkbox') {
   if (a[i].checked) {
        
        for (var j = 0; j<v.length; j++) {
        if (v[j].type == 'checkbox') {
         if (v[j].checked) {
              alert("it werks!!")
              return true;
            }
          }
        }
        return true;
      } else {
       return false;
      }
    }
  }
}
<form id="frmForm" name="frmForm" action="#" method="post" onsubmit="return Validation()">
    <div class="row" style="margin-top: 20px;">
    <div class="col-sm-12">
     <div class="form-group">
      <p>
     <b>1. Which of the volunteer jobs did you like the most in the year 2018?</b>
     </p>
     <p style="margin-left: 16px;">
     (You can choose one or more options as your answer)
     </p>
     <div style="margin-left: 35px;">
       <input type="checkbox" id="Activity1" name="Activity" value="supplies" > 1<br>
      <input type="checkbox" id="Activity2" name="Activity" value="food" > 2<br>
      <input type="checkbox" id="Activity3" name="Activity" value="plastic" > 3<br>
      <input type="checkbox" id="Activity4" name="Activity" value="clean" > 4<br>
      <input type="checkbox" id="Activity5" name="Activity" value="painting" > 5<br>
      <input type="checkbox" id="Activity6" name="Activity" value="zmps" > 6<br>
      <input type="checkbox" id="Activity7" name="Activity" value="sumica" > 7<br>
      <input type="checkbox" id="Activity8" name="Activity" value="blood" > 8<br>
      <input type="checkbox" id="Activity9" name="Activity" value="volunteer" > 9<br>
     </div>
    </div>
   </div>
  </div>
    <div class="row" style="margin-top: 20px;">
    <div class="col-sm-12">
     <div class="form-group">
      <p>
     <b>2. How much time are you prepared to spend as volunteer?</b>
     </p>
     <p style="margin-left: 16px;">
     (You can only choose one option as your answer)
     </p>
     <div style="margin-left: 35px;">
       <input type="radio" id="Volunteers1" name="Volunteers" value="oneWeek" > 1<br>
      <input type="radio" id="Volunteers2" name="Volunteers" value="fiveWeek" > 2<br>
      <input type="radio" id="Volunteers3" name="Volunteers" value="oneMonth" > 3<br>
      <input type="radio" id="Volunteers4" name="Volunteers" value="fivemonth" > 4<br>
      <input type="radio" id="Volunteers5" name="Volunteers" value="halfYear" > 5<br>
      <input type="radio" id="Volunteers6" name="Volunteers" value="twoYear" > 6<br>
      <input type="radio" id="Volunteers7" name="Volunteers" value="other" > 
      <input type="text" placeholder="other" /><br>
     </div>
    </div>
   </div>
  </div>
  <button style="margin-top:30px;"type="submit" id="Submit" name="Submit">Send me away</button>
    <div id="errorBox" style="margin-bottom:50px; color: red"></div>
</form>

What I am trying to do, is make a function that will check the form. The given code is, of course, just a demo, as my form is more extensive. The code was supposed to check an answer and if checked, check the next one, and so on... I have tried to put if statements separated, but that only caused that after just first answer being checked, the code would send the message as if the whole form was filled.

And while we are at it, what if a form had a question with checkbox answer, saying other (write your answer), how would I make a code that would first check if the checkbox was checked and afterwards check if the input was also given?

I will really appreciate any help on the second part of the question, as well as for finding an error and correcting me on first part :)

EDIT:: Tested the code, using the console.log and only after all the checkboxes in first question and a radio in second question were selected, the form was tagged as correctly answered.

Another conclusion, it appears that for some reason, only for some answers in first question, the code will think they were not checked, while for others (like first answer), the code would accept it and continue as form correctly answered. Any explanation to this?

Newbie J
  • 186
  • 1
  • 19
  • I would appreciate if, whoever voted this question -1, would point out what bothers them, so I can learn what I did wrong, otherwise that kind of move doesn't really make any sense... – Newbie J Feb 15 '19 at 09:16
  • May be this https://stackoverflow.com/questions/20068487/getting-multiple-selected-checkbox-values-in-a-string-in-javascript-and-php can help you! – Maria Irudaya Regilan J Feb 15 '19 at 09:22
  • @MariaIrudayaRegilanJ, Thank you, will check it out right away. – Newbie J Feb 15 '19 at 09:27

3 Answers3

1

I have checked your code and it has issue with the loops because you are checking

if (a[i].checked) {

and returning false if not checked . This is the issue, it will not check for next activity checkboxes if first checkbox is not checked.

Shadab Ahmed
  • 556
  • 9
  • 20
  • at first I didn't really understand what you were trying to say, then after typing my own answer, I got your message :) Thanks for pointing it out and for time :) – Newbie J Feb 15 '19 at 10:58
  • will make sure my answers are more clear in future :p – Shadab Ahmed Feb 15 '19 at 14:12
0

So if I see this correctly, then 'Volunteers' if of type 'radio', but in the code you're checking for 'checkbox'. Think this should clearly be leading to exiting the second loop always.

For quick debugging, I also suggest to put some console.log statements, so you can see in the browser console how far your code gets...

Erik Reder
  • 303
  • 3
  • 14
  • even after editing the "checkbox" to "radio" at that question, the problem seems to still be there, will try the console.log tho, thanks for the suggestion – Newbie J Feb 15 '19 at 09:13
  • Mhm, I just ran the code snippet here, and I got the message "Thank you for the cooperation" - that's the expected result, right?! – Erik Reder Feb 15 '19 at 09:28
  • that should be it, yes. Did you by any chance change anything? I am still getting problems somehow... – Newbie J Feb 15 '19 at 09:31
  • I have checked the snipped myself and it seems for some reason, most of the answers will not trigger the code, making it think they were not even selected... don't have a clue, what is going on – Newbie J Feb 15 '19 at 09:46
0

So I went over the code and found out that the problem were the return false and return true,

New, working, correctly, code is the following:

function Validation() {
  if(!ValidateForm()) {
    document.getElementById("errorBox").innerHTML = "Please double check, that you have answered all the questions and try again."
      return false;
  } else {
    document.getElementById("errorBox").innerHTML = ""
    alert("Thank you for your cooperation");      
  }
}

function ValidateForm() {
  var a = document.getElementsByName('Activity');
  var v = document.getElementsByName('Volunteers');           
  for (var i = 0; i<a.length; i++) {
    //console.log(a[i]);
    if (a[i].checked) {
      for (var j = 0; j<v.length; j++) {
        if (v[j].checked) {
          console.log(a[i],",",v[j])
          return true;
        }
      }
    }
  }
}
<form id="frmForm" name="frmForm" action="#" method="post" onsubmit="return Validation()">
<div class="row" style="margin-top: 20px;">
  <div class="col-sm-12">
    <div class="form-group">
     <p>
     <b>1. Which of the volunteer jobs did you like the most in the year 2018?</b>
     </p>
     <p style="margin-left: 16px;">
        (You can choose one or more options as your answer)
     </p>
       <div style="margin-left: 35px;">
         <input type="checkbox" id="Activity1" name="Activity" value="supplies" > 1<br>
         <input type="checkbox" id="Activity2" name="Activity" value="food" > 2<br>
         <input type="checkbox" id="Activity3" name="Activity" value="plastic" > 3<br>
         <input type="checkbox" id="Activity4" name="Activity" value="clean" > 4<br>
         <input type="checkbox" id="Activity5" name="Activity" value="painting" > 5<br>
         <input type="checkbox" id="Activity6" name="Activity" value="zmps"> 6<br>
         <input type="checkbox" id="Activity7" name="Activity" value="sumica"> 7<br>
         <input type="checkbox" id="Activity8" name="Activity" value="blood"> 8<br>
         <input type="checkbox" id="Activity9" name="Activity" value="volunteer"> 9<br>
       </div>
      </div>
    </div>   
  </div>   
  <div class="row" style="margin-top: 20px;">
    <div class="col-sm-12">
      <div class="form-group">
        <p>
        <b>2. How much time are you prepared to spend as volunteer?</b>
        </p>
        <p style="margin-left: 16px;">
          (You can only choose one option as your answer)
        </p>
        <div style="margin-left: 35px;">
          <input type="radio" id="Volunteers1" name="Volunteers" value="oneWeek" > 1<br>
          <input type="radio" id="Volunteers2" name="Volunteers" value="fiveWeek" > 2<br>
          <input type="radio" id="Volunteers3" name="Volunteers" value="oneMonth" > 3<br>
          <input type="radio" id="Volunteers4" name="Volunteers" value="fivemonth" > 4<br>
          <input type="radio" id="Volunteers5" name="Volunteers" value="halfYear" > 5<br>
          <input type="radio" id="Volunteers6" name="Volunteers" value="twoYear" > 6<br>
          <input type="radio" id="Volunteers7" name="Volunteers" value="other" > 
          <input type="text" placeholder="other" /><br>
        </div>
      </div>
    </div>
  </div>
  <button style="margin-top:30px;"type="submit" id="Submit" name="Submit">Send me away</button>
  <div id="errorBox" style="margin-bottom:50px; color: red"></div>
</form>
Newbie J
  • 186
  • 1
  • 19