All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Validate Checkbox In JavaScript

Last Updated : Mar 11, 2024

How To Validate Checkbox In JavaScript

In this tutorial we will show you the solution of how to validate checkbox in JavaScript, here we defined label with some options, those options we provide with checkbox type we need to validate whether users will choose their choices or not by checking whether all checkbox is empty or not using javascript.

Then respective message will alert users if they not provide any answer then we need to tell them to choose any one.

Step By Step Guide On How To Validate Checkbox In JavaScript :-

Here we defined one label ‘Select Color’ and three options ‘Red,Rose,Green’ for that then submit button.

When user clicks on submit it loads ‘func()’ function call defined in onclick event.

Within function we using if() condition with three options value whether not checked then display message ‘Select Anything’ using alert() method, otherwise it will display ‘Submitted’ message on webpage.

<!DOCTYPE html>
<html>
    <head>
        <title>VALIDATE CHECKBOX IN JS</title>
    </head>
    <body>
        <label>Select Color :</label><br>
        <input type="checkbox" id="cb1"> Rose<br>
        <input type="checkbox" id="cb2"> Red<br>
        <input type="checkbox" id="cb3"> Green<br><br>
        <input type="submit" value="Submit" onclick="func()">
        <script>
            function func(){
                if(!document.getElementById('cb1').checked && !document.getElementById('cb2').checked &&
                !document.getElementById('cb3').checked){
                    alert("Select Anything")
                }
                else{
                    alert("Submitted")
                }
            }
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined label ‘Select Color’ using <label> tag then <br> used to break the line. Input tag defined with checkbox type and values ‘Red,Rose,Green’ then each one had their id attribute with different value for check whether checked or not.
  7. At last we defined submit button with onclick event for initialize func() function call when user submit their response.
  8. In script we defined func() function, here we checks each checkbox by their id values ‘cb1,cb2,cb3’ not checked if it is true then it display message ‘Select Anything’ on alert box otherwise it display ‘Submitted’.
  9. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to validate checkbox in javascript.

When we executes our program on browser we can see one label ‘select color’ with three checkbox options user needs to select any options if they not provide any answer and submitted then it will notify user to select anything otherwise it will display submitted their result on webpage.

We can also create with different values with checkbox.

I hope this tutorial on how to validate checkbox in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪