Javascript DOM HTML Input DatetimeLocal required Property set

Introduction

Set a local datetime field to be a required field of form submission:

document.getElementById("myLocalDate").required = true;

Click the "Test" button to set the required property for the datetime field.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
  Date: <input type="datetime-local" id="myLocalDate" name="date">
  <input type="submit">
</form>/*w ww  .ja v  a  2 s .c  om*/
<button onclick="myFunction()">Test</button>
<p id="demo"></p>

<script>
function myFunction() {
  document.getElementById("myLocalDate").required = true;
  document.getElementById("demo").innerHTML = "The required property was set.";
}
</script>

</body>
</html>



PreviousNext

Related