The required
property sets or gets
whether a date field must be filled out before submitting a form.
required |
Yes | No | No | Yes | Yes |
Return the required property.
var v = dateObject.required
Set the required property.
dateObject.required=true|false
Value | Description |
---|---|
true|false | Specifies whether a date field is required for form submission.
|
A Boolean type value, true if the date field is required for form submission, otherwise it returns false.
The following code shows how to mark a date field as required.
<!DOCTYPE html>
<html>
<body>
<!--from w w w. j a v a 2s. c om-->
<form action="form_action.asp">
Date: <input type="date" id="myDate" name="date">
<input type="submit">
</form>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("myDate").required = true;
document.getElementById("demo").innerHTML = "required";
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to check if a date field must be filled before submitting a form.
<!DOCTYPE html>
<html>
<body>
<!-- ww w . j a v a 2s . com-->
<form action="form_action.asp">
Date: <input type="date" id="myDate" name="date" required>
<input type="submit">
</form>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myDate").required;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
The code above is rendered as follows: