CrazyEngineers Archive
Old, but evergreen and popular discussions on CrazyEngineers, presented to you in read-only mode.
@Shweta Chauhan • 22 May, 2016 • 1 like
I am trying to work out this code, but this thing is not working!
Javascript:
if(form.interest[0].checked==false && form.interest[1].checked==false && form.interest[2].checked==false && form.interest[3].checked==false)
{
alert("Please Check");
return false;
}
HTML:
<input type="checkbox" name="interest[]" value="News"/>News
        <input type="checkbox" name="interest[]" value="Health"/>Health
        <input type="checkbox" name="interest[]" value="Automobiles"/>Automobiles
        <input type="checkbox" name="interest[]" value="Internet"/>Internet
Please guide and tell me how to do multiple checkbox validation using javascript.
@Kaustubh Katdare • 22 May, 2016 • 1 like I'd strongly using jQuery unless you've special interest in learning plain vanilla JS. That said, you should always look at the 'Console' in Chrome by inspecting your page.

I'm not well versed with JavaScript; but here's a working example of your code.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CheckBox Validation</title>
    <script type="text/javascript" language="JavaScript">

        function checkCheckBoxes(theForm) {

            var test = document.getElementsByName("interest[]");

            if(test[0].checked==false && test[1].checked==false && test[2].checked==false)
            {
                alert("Please Check");
                return false;
            }

        }

    </script>
</head>
<body>

        <form action="" onsubmit="return checkCheckBoxes(this)" name="checkForm">

            <input type="checkbox" name="interest[]" value="News"/>News
            <input type="checkbox" name="interest[]" value="Health"/>Health
            <input type="checkbox" name="interest[]" value="Automobiles"/>Automobiles

            <p><input type="SUBMIT" value="Submit!"></p>

        </form>

</body>
</html>
@Anoop Kumar • 23 May, 2016 • 1 like If you really want to go with only javascript to build the foundation of scripting. Here is the code.
HTML in Body tag

<form name="fname">

        <input type="checkbox" name="interest" value="News" onclick="checkCheckBox()"/>News
        <input type="checkbox" name="interest" value="Health" onclick="checkCheckBox()"/>Health
        <input type="checkbox" name="interest" value="Automobiles" onclick="checkCheckBox()"/>Automobiles
        <input type="checkbox" name="interest" value="Internet" onclick="checkCheckBox()"/>Internet

    </form>
Javascript code inside Head tag
<script type="text/javascript">
        function checkCheckBox() {
           var chk_arr = document.fname.interest;
           for(var i = 0; i < chk_arr.length; i++){
               if(chk_arr[i].checked) {
                   console.log(chk_arr[i].value);
                   alert(chk_arr[i].value);
                }
            }
        }
    </script>
Problems:
  1. You are not using document (primary node) to find the elements in page.
  2. How does javascript will be called. You need call piece of script (say a function) to execute on an event. Such as checking and unckecking of a checkbox.
10.3k views

Related Posts

@Ankita Katdare · Oct 21, 2011

What is the recruitment procedure for Jindal Steel and Power? Does it visit campus regularly? What is the placement criteria? Please share Placement Papers if any.
4.4k views

@Rudresh008 · Aug 9, 2011

Anybody can provide me the link or pdf of Biomedical instrumentation book by R.S.Khandpur TMH plssssssss
11.5k views

@durga ch · Nov 18, 2009

Many might have already come across these words: but just a repea fort hose who dont know this man's speech! frankly speaking, Wipro Technologies is a competitive vendor and though...
4.4k views

@m.vivekanandan · Apr 13, 2012

can a 3rd yr student apply as if he is a final year and write GATE to gain experience????
6.2k views

@hare singh nayak · Sep 8, 2012

The Gini coefficient measures the inequality among values of a frequency distribution (for example levels of income). A Gini coefficient of zero expresses perfect equality where all values are the...
18.3k views