Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck boxes validation in JavaScript
    text
    copied!<p>I have written this script:</p> <pre><code>function getDays(select){ var selectedString = select.options[select.selectedIndex].value; if(selectedString == 4) { document.getElementById("days_target").style.display = "block"; }else { document.getElementById("days_target").style.display = "none"; } } </code></pre> <p>and in <code>validateForm()</code> function I have this:</p> <pre><code>var x=document.forms["form1"]["days"].value; if (x==null || x=="" || x=="Select Days") { alert("Oh, you forgot to select days! :)"); return false; } var x=document.forms["form1"]["days"].value; if(x=="4") { var cnt = 0; for (var i = 7; i &lt; document.day.elements.length; i++) { if (document.day.elements[i].type == 'checkbox') { if (document.day.elements[i].checked == true) { cnt++; } } } if (cnt == 0) { alert("Atleast 1 day Should be Selected."); return false; } } </code></pre> <p>HTML like this:</p> <pre><code>&lt;b&gt;Please enter days required&lt;/b&gt;&lt;br/&gt; &lt;select name="days" id="days" style="width:200px;" onchange="getDays(this)"&gt; &lt;option value="Select Days" selected&gt;Select Days&lt;/option&gt; &lt;option value="1"&gt;Mon-Fri&lt;/option&gt; &lt;option value="2"&gt;Mon-Fri&lt;/option&gt; &lt;option value="3"&gt;Mon-Fri&lt;/option&gt; &lt;option value="4"&gt;Bespoke Days&lt;/option&gt; &lt;/select&gt;&lt;br/&gt;&lt;br/&gt; &lt;div id="days_target" style="display:none;"&gt; &lt;b&gt;Select Days&lt;/b&gt;&lt;br/&gt; &lt;input type="checkbox" name="day" value="mon"/&gt;Mon &amp;nbsp;&amp;nbsp;&lt;input type="checkbox" name="day" value="tue"/&gt;Tue&lt;br/&gt; &lt;input type="checkbox" name="day" value="wed"/&gt;Wed &amp;nbsp;&amp;nbsp;&lt;input type="checkbox" name="day" value="thr"/&gt;Thr&lt;br/&gt; &lt;input type="checkbox" name="day" value="fri"/&gt;Fri &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;input type="checkbox" name="day" value="sat"/&gt;Sat&lt;br/&gt; &lt;input type="checkbox" name="day" value="sun"/&gt;Sun&lt;br/&gt;&lt;br/&gt; &lt;/div&gt; </code></pre> <p>If I select Bespoke days then that check boxes appear and if none is checked then I want to display error message "Atleast one day should be selected." How to do this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload