The required
property sets or gets
whether a local datetime field must be filled before submitting a form.
required |
Yes | No | No | Yes | Yes |
Return the required property.
var v = datetimelocalObject.required
Set the required property.
datetimelocalObject.required=true|false
Value | Description |
---|---|
true|false | Set whether a local datetime field is a required part of the form.
|
A Boolean type value, true if the local datetime field is a required part of form submission, otherwise false.
The following code shows how to set a local datetime field to be a required part of form submission.
<!DOCTYPE html>
<html>
<body>
<!-- w ww.ja va2 s .c om-->
<form action="url">
Date: <input type="datetime-local" id="myLocalDate" name="date">
<input type="submit">
</form>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("myLocalDate").required = true;
document.getElementById("demo").innerHTML = "changed";
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to check if a local datetime field must be filled before submitting a form.
<!DOCTYPE html>
<html>
<body>
<form action="url">
Date: <input type="datetime-local" id="myLocalDate" name="date" required>
<input type="submit">
</form><!--from www.j a va2 s. c o m-->
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myLocalDate").required;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
The code above is rendered as follows: