All TalkersCode Topics

Follow TalkersCode On Social Media

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

JavaScript Radio Button Validation

Last Updated : Jul 1, 2023

JavaScript Radio Button Validation

In this tutorial we will show you the solution of JavaScript radio button validation, here we are validating radio button by using checked statement in javascript.

The checked attribute is a boolean attribute. When present, it specifies that an input element should be pre-selected (checked) when the page loads.

So we can use that attribute to find outs radio button states whether checked by user or not.

Step By Step Guide On JavaScript Radio Button Validation :-

Here we defined two radio buttons and submit button. In script we collected both radio buttons id’s ‘rbtn1,rbtn2’ for access radio button attribute values and stored on respective variables ‘s,n’.

Using if condition we checking whether both radio buttons not selected then we are throwing alert message to notify users needs to select option otherwise throwing ok message using alert() method.

<!DOCTYPE html>
<html>
    <head>
        <title>RADIO BUTTON VALIDATION</title>
    </head>
    <body>
        <input type="radio" name="r1" id="rbtn1">Yes
        <input type="radio" name="r1" id="rbtn2">No
        <br><br><br>
        <button id="sub">Submit</button>
        <script>
            document.getElementById("sub").addEventListener("click",function(){
                var s=document.getElementById("rbtn1");
                var n=document.getElementById("rbtn2");
                if(!s.checked && !n.checked){
                    alert("Plz select");
                }
                else{
                    alert("OK :-)");
                }
            })
        </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 contain 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 first two input elements defined in radio type with text ‘YES,NO’ and it have id attribute with values ‘rbtn1,rbtn2’.
  7. A submit button defined with id ‘sub’ for proceeds some process when user clicks on it.
  8. In script we referring submit button by id ‘sub’ which is appends with ‘addEventListener’ for verify when user clicks on it.
  9. If user clicks on submit button then it loads ‘click’ event, there we collecting radio button id’s ‘rbtn1,rbtn2’ for access them and stored on respective variables ‘s,n’.
  10. Using if condition we checking whether both radio buttons not checked by user by ‘!s.checked && !n.checked’ condition statement. If they returns true then alert box appear with message ‘Plz select’.
  11. Otherwise it returns false so else part will work it throws ‘OK :-)’ message on alertbox by alert() method.
  12. 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 radio button using javascript.

When we executes program on browser we can see two radio buttons with option ‘Yes, No’ and a Submit button user must needs to select any one radio button then clicks on Submit button respective alert message will appear for user clarification.

If you’re needs to change radio button label just modify that text we get output accordingly.

I hope this tutorial on JavaScript radio button validation helps you and the steps and method mentioned above are easy to follow and implement.