Multiple Radio Button Validation In JavaScript
Last Updated : Mar 11, 2024
IN - JavaScript | Written & Updated By - Amruta
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>
- <!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.
- The<html> tag is used to indicate the beginning of HTML document.
- 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.
- 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.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- 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’.
- Finally we defined submit button with ‘Validate()’ function call and <div> tag defined for appends result message on webpage.
- In <script> we defined function Validate(), here we collected all defined input tags in html body and stored variable ‘ele’.
- 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.
- 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.