Javascript Reference - HTML DOM Input Radio required Property








The required property sets or gets whether a radio button must be filled before submitting a form.

Browser Support

required Yes 10.0 Yes Yes Yes

Syntax

Return the required property.

var v = radioObject.required 

Set the required property.

radioObject.required=true|false

Property Values

Value Description
true|false Set whether a radio button should be checked before submitting a form
  • true - The radio button is a required field
  • false - Default. The radio button is not a required field




Return Value

A Boolean type value, true if the radio button must be checked before submitting a form, otherwise false.

Example

The following code shows how to set a radio button to be a required part of form submission.


<!DOCTYPE html>
<html>
<body>
<!--   w  w w  .jav  a  2 s.  c  om-->
<form action="url">
  Radio Button: <input type="radio" id="myRadio" name="test">
  <input type="submit">
</form>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myRadio").required = true;
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to check if a radio button must be checked before submitting a form.


<!DOCTYPE html>
<html>
<body>
<!-- w  w w .jav  a  2s .  com-->
<form action="url">
  Radio Button: <input type="radio" id="myRadio" name="test" required>
  <input type="submit">
</form>
<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myRadio").required;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: