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.
8.6k views

Related Posts

@Ankita Katdare · Dec 5, 2013

Maxx Mobiles has launched a new feature phone at dirt cheap price targeted at rural markets in India with - Maxx MX100 Leader Feature phone. In fact, Telecom Minister Kapil...
5.8k views

@Dipankar Ganguly · Nov 4, 2017

Every year India produces around 1.5 million engineers from its 6000+ engineering and technology institutions, which is more engineers than the USA and China combined. It seems like a wonderful...
3.9k views

@Vishal Sharma · Jun 17, 2012

Hello Everyone!! i'm working on this C program which deals with user registration and login page. I was successful is writing the program for only 1 user. But now when...
32.5k views

@Debasmita Banerjee · Mar 1, 2017

A couple of weeks back, I saw my 8-year-old cousin heavily concentrating on a hole near the gateway to the kitchen and counting something invisible to my eyes. I approached...
3.2k views

@Kaustubh Katdare · Jan 9, 2015

One of the most frequently asked question, typically in HR interview is straightforward: "Tell Me About Yourself". Almost all the HR interviews I've faced and have been a part of...
34k views