All TalkersCode Topics

Follow TalkersCode On Social Media

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

Radio Button Validation In JavaScript

Last Updated : Mar 11, 2024

Radio Button Validation In JavaScript

In this article we will show you the solution of radio button validation in JavaScript, we use the radio button in an HTML form to select one option from a list.

Now we can validate the radio button submission using JavaScript.

We will see one example to validate the radio button using For loop below.

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

<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> radio button validation in javascript </title>
    <style>
        body {
            font-family : 'Lucida Sans', Verdana , sans-serif ;
        }
        h1 {
         font-size : larger ;
         font-weight : bolder ;
         color : rgb(113, 221, 113) ;
         text-align : center ;
        }
        h3 {
            text-align : center ;
        }
    </style>
    <script>
        function validate() {
            var valid = false ;
            var x = document.myForm.color ;
            for (var i=0;i<x.length;i++){
                    if(x[i].checked) {
                        valid = true;
                        break;
                    }
            }
            if (valid) {
                alert (" Validation Successful ") ;
            } else {
                alert (" Please Select a colour ") ;
                return false ;
            }
        }
    </script>
</head>
<body>
    <h1> TALKERSCODE </h1>
    <h3> radio button validation in javascript </h3>
     <p> Select a colour: </p>
    <form action = "dropdown.php" name = "myForm" onsubmit = "return validate()">
        <input type="radio" name="color" value="red"> Red
        <input type="radio" name="color" value="blue"> Blue
        <input type="radio" name="color" value="green"> Green
        <input type="radio" name="color" value="yellow"> Yellow
        <input type="submit" value="submit"/>
    </form>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Now create a < style > tag to add style to the HTML page.
  6. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  7. <h1> tag used to add heading here.
  8. Create a <form> with the action of a php file, the name of ‘myForm’, and an onsubmit of a function return.
  9. Now create some <input> tags of the type ‘radio’, and add the name of ‘color’ in all of them.
  10. Also added <input> tag for submit with the value of ‘submit’
  11. Now create a <script> tag to write the JavaScript file. And close it with </script> tag.
  12. Into that <script> tag, create a function named validate (). At first, create a variable named valid equal to false.
  13. Create another variable x to get the document.myForm.color.
  14. For the loop to check if the valid is equal to true, then break the loop
  15. Now create an if-else statement, to check if the radio button validation is valid the display an alert of “validation successful” and if not display “please select a color”.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to radio button validation using JavaScript.

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

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪