All TalkersCode Topics

Follow TalkersCode On Social Media

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

Multiple Radio Button Validation In JavaScript

Last Updated : Mar 11, 2024

Multiple Radio Button Validation In JavaScript

In this tutorial we will show you the solution of multiple radio button validation in JavaScript, here we defined two group of radio buttons and submit button.

When user clicks submit button it loads function ‘Validate()’.

The Validate() method collects all radio inputs using for loop then checks which are selected from that and displays those result on webpage using ‘innerHTML’ in javascript.

Step By Step Guide On Multiple Radio Button Validation In JavaScript :-

Here we defined two group of radio namely ‘Colors,Foods’ with respective options and ‘submit’ button, div tag for display results. In script Validate() function defined, here we collected all input tags and stored to variable ‘ele’.

For iterate all input fields we used for loop then we identified ‘radio’ types only from that after that we collected selected radio buttons.

Finally we appends those selected radio buttons results on <div> tag with id ‘res’.

<!DOCTYPE html>
<html>
    <head>
        <title>Group Of Radio Buttons</title>
    </head>
    <body>
            <p>Select Colors</p>
            <input type="radio" name="Color" value="Red" > Red
            <input type="radio" name="Color" value="Rose"> Rose
            <input type="radio" name="Color" value="Green"> Green
            <p>Select Foods</p>
            <input type="radio" name="Food" value="Parota" > Parota
            <input type="radio" name="Food" value="Dosa"> Dosa
            <input type="radio" name="Food" value="Briyani"> Briyani
            <br>
            <button onclick="Validate()" style="margin-top: 2rem;">Submit</button>
        <div style="margin-top: 2rem;" id="res"></div>
        <script>
            function Validate(){
                var ele=document.getElementsByTagName("input");
                for(var i=0;i<ele.length;i++){
                    if(ele[i].type='radio'){
                    if(ele[i].checked){
                        document.getElementById('res').innerHTML+=ele[i].name+' Value: '+ele[i].value+'<br>';
                    }}
                }
            }
        </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 two groups of radio buttons. First group we defined ‘Colors’ with options ‘Red,Rose,Green’ and second group is ‘Foods’ with values ‘Parota,Dosa,Briyani’.
  7. Finally we defined submit button with ‘Validate()’ function call and <div> tag defined for appends result message on webpage.
  8. In <script> we defined function Validate(), here we collected all defined input tags in html body and stored variable ‘ele’.
  9. Using for we iterated all input all input tags the using if condition we checks which input tag had radio button type those are send to another if condition for checks which radio button checked by user then those are appends to ‘res’ id of div tag.
  10. 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 multiple radio button in javascript.

When we executes our program on browser we can see two group of radio buttons user can select any option from those two sets then when user clicks button ‘submit’ event ‘onclick’ loads function Validate() then displays selected options as result on webpage and we can also display results on alertbox or console.

Later we will see about them I hope this tutorial on multiple radio button validation in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.