All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Radio Button Validation In JavaScript For Gender

Last Updated : Jul 1, 2023

Radio Button Validation In JavaScript For Gender

In this tutorial we will show you the solution of radio button validation in JavaScript for gender, 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 Radio Button Validation In Javascript For Gender :-

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 any option otherwise throwing ok message using alert() method.

<!DOCTYPE html>
        <input type="radio" name="r1" id="rbtn1">Female
        <input type="radio" name="r1" id="rbtn2">Male
        <button id="sub">Submit</button>
                var s=document.getElementById("rbtn1");
                var n=document.getElementById("rbtn2");
                if(!s.checked && !n.checked){
                    alert("Plz select your gender");
                    alert("Valid Input :-)");
  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 ‘Female,Male’ 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 your gender’.
  11. Otherwise it returns false so else part will work it throws ‘Valid Input :-)’ 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 with gender label using javascript.

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

If you’re needs to change alert message just modify that text we gets output message accordingly.

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