Input Text required Attribute

Definition and Usage

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

This attribute reflects the HTML required attribute.

See also:

HTML Reference Manual:HTML required 属性

Example

Example 1

Determine whether the text field must be filled out before submitting the form:

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

Try it yourself

Example 2

Set the text field as a required part of form submission:

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

Try it yourself

Syntax

Return the required attribute:

textObject.required

Set the required attribute:

textObject.required = true|false

Attribute value

Value Description
true|false

Specify whether the text field should be a required part of form submission.

  • true - text field is a required part of form submission
  • false - 默认。文本字段不是表单提交的必填部分

技术细节

返回值: 布尔值,如果文本字段是表单提交的必填部分,则返回 true;否则返回 false

浏览器支持

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

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