HTML 5 custom validation for radio button group not working

Viewed 1183

I want to add a custom validation message to my radio buttons group that I have.

It works fine in the below conditions:

  1. If I select the second radio button "female" and click on submit.
  2. If I select the first radio button "male" and click on submit.
  3. If I do not select any radio button and click on submit, it shows me the validation message. On selecting the first radio button and clicking submit, the form is submitted.

It does not work in the below condition:

  1. If I click on submit without checking any radio button, it shows me the validation message. After that if I select the second radio button "female" and click on submit, it still shows me the validation message on the first radio button "male".

Below is the code I have written

<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body>
    <div id="overlay">
    </div>
    <div class="container">
      <form id="myForm">
        <div class="row">
          <input type="radio" name="gender" value="male" required 
          oninvalid="this.setCustomValidity('Please select your gender')"
          oninput="this.setCustomValidity('')">
          <label for="male">Male</label>
          <br>
          <input type="radio" name="gender" value="female" required
          oninvalid="this.setCustomValidity('Please select your gender')"
          oninput="this.setCustomValidity('')">
          <label for="female">Female</label>
          <br>
         </div>
        <div class="row">
          <div class="col">
            <input class="submit_button" type="submit" value="Submit">
          </div>
        </div>
      </form>
    </div>
  </body>
</html>

I know this custom validation issue can be fixed by writing a function in javascript/jquery. But I would like to know if there is a way to do this inline or if there is any documentation that states it cannot be done.

Note : If I do not put the custom validation code oninvalid="this.setCustomValidity('Please select your gender')" oninput="this.setCustomValidity('')". All the conditions work correctly.

2 Answers

You can do this.

<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body>
    <div id="overlay">
    </div>
    <div class="container">
      <form id="myForm">
        <div class="row">
          <input type="radio" name="gender" id="gender" value="male">
          <label for="male">Male</label>
          <br>
          <input type="radio" name="gender" id="gender" value="female">
          <label for="female">Female</label>
          <br>
         </div>
        <div class="row">
          <div class="col">
            <input class="submit_button" type="submit" value="Submit" onClick="ValidateForm(this.form)">
          </div>
        </div>
      </form>
    </div>

<script type="text/javascript">
    function ValidateForm(form){
        ErrorText= "";
        if ( ( form.gender[0].checked == false ) && ( form.gender[1].checked == false ) )
        {
        alert ( "Please select your gender" );
        return false;
        }
        if (ErrorText= "") { form.submit() }
    }
</script>
  </body>
</html>

Your code:

      <input type="radio" name="gender" value="male" required 
      oninvalid="this.setCustomValidity('Please select your gender')"
      oninput="this.setCustomValidity('')">

      <input type="radio" name="gender" value="female" required
      oninvalid="this.setCustomValidity('Please select your gender')"
      oninput="this.setCustomValidity('')">

try this instead (no JS technically):

    <input type="radio" name="gender" value="male" required
           oninvalid="if(!document.querySelector('input[name=\'gender\'][checked=true]').length >= 1)this.setCustomValidity('Please select your gender')"
           oninput="this.setCustomValidity('')">

    <input type="radio" name="gender" value="female" required
           oninvalid="if(!document.querySelector('input[name=\'gender\'][checked=true]').length >= 1)this.setCustomValidity('Please select your gender')"
           oninput="this.setCustomValidity('')">

Working link

Related