Input Datetime required attribute

Definition and Usage

required Attribute sets or returns whether the date and time field must be filled out before submitting the form.

This property reflects the HTML required attribute.

See also:

HTML Reference Manual:HTML <input> required 属性

Example

Example 1

Determine whether the date and time field must be filled out before submitting the form:

var x = document.getElementById("myDatetime").required;

Try it yourself

Example 2

Set the date and time field as a required part of the form submission:

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

Try it yourself

Syntax

Return the required attribute:

datetimeObject.required

Set the required attribute:

datetimeObject.required = true|false

Attribute value

Value Description
true|false

Specify whether the date and time field should be a required part of the form submission.

  • true - The date and time field is a required part of the form submission
  • false - Default. The date and time field is not a required part of the form submission

Technical details

Return value: Boolean value, if the date and time field is a required part of the form submission, it returns true;否则返回 false

浏览器支持

表中的数字注明了首个完全支持该属性的浏览器版本。

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
支持 10.0 支持 支持 支持

注意:<input type="datetime"> 元素在除 Safari 之外的任何主流浏览器中均不显示任何日期时间字段/日历。