html required radio input code example

Example 1: radio button required

One input with required attribute is enough.

<form>
  Select Gender:

  <label><input type="radio" name="gender" value="male" required>Male</label>

  <label><input type="radio" name="gender" value="female">Female</label>

  <label><input type="radio" name="gender" value="other">Other</label>

  <input type="submit">
</form>

Example 2: how to put required in radio button

<form method="post" action="/Tests/Post" onsubmit="return ValidateForm();">    <fieldset>    <legend>What is Your Favorite Pet?</legend>    <input type="radio" name="favorite_pet" value="Cats" checked>Cats<br>    <input type="radio" name="favorite_pet" value="Dogs">Dogs<br>    <input type="radio" name="favorite_pet" value="Birds">Birds<br>        <br>    <input type="submit" value="Submit now">    </fieldset></form><script type="text/javascript">function ValidateForm(){    var radioButtons = document.getElementsByName("favorite_pet");    for(var i = 0; i < radioButtons.length; i++)    {        if(radioButtons[i].checked == true)        {            if(confirm("You have selected " + radioButtons[i].value + " as your favorite pet. Is that correct?"))                return true;            else                return false;        }    }}</script>

Tags:

Html Example