Monday, 24 October 2016

jQuery: Validate, Test or Check if Checkbox or Checkboxes is Checked and Get Checkbox Value

10/24/2016 - By Pranav Singh 0

This article will show you how you can validate given checkbox or checkboxes are checked or not and get the checked checkbox value using jquery.

So for this article first we will take some checkboxes and assign some values to it.

<input id="chk1" type="checkbox" name="count" value="1">
<input id="chk2" type="checkbox" name="count" value="2">
<input id="chk3" type="checkbox" name="count" value="3">
<input id="chk4" type="checkbox" name="count" value="4">
<input id="chk5" type="checkbox" name="count" value="5">
<input id="chk6" type="checkbox" name="count" value="6">

After this we will add the jQuery library reference.

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>

Now check the code which we will use for validating and getting the value of selected checkboxes.

  $(document).ready(function () {
            $("#btnValidate").click(function () {
                $('input[name="count"]:checked').each(function () {
                    alert(this.value);
                });
            });
        });

In above code I have validated each and every checkbox provided, And if it is checked on that case it’s value have been displayed in the message. If you click on button without selected nothing will happen.

Here is the complete code.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>jQuery: Validate, Test  or Check if Checkbox or Checkboxes is Checked and Get Checkbox Value</title>
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script>
        $(document).ready(function () {
            $("#btnValidate").click(function () {
                $('input[name="count"]:checked').each(function () {
                    alert(this.value);
                });
            });
        });
    </script>

</head>
<body>
    <input id="chk1" type="checkbox" name="count" value="1">One <br />
    <input id="chk2" type="checkbox" name="count" value="2">Two<br />
    <input id="chk3" type="checkbox" name="count" value="3">Three<br />
    <input id="chk4" type="checkbox" name="count" value="4">Four<br />
    <input id="chk5" type="checkbox" name="count" value="5">Five<br />
    <input id="chk6" type="checkbox" name="count" value="6">Six<br />

    <input type="button" value="Validate Checkbox" id="btnValidate" />
</body>
</html>


Now we have done run the application and check the output.





About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top