HTML Required 属性是一个布尔属性,用于指定在提交表单之前必须填写输入元素。该属性与以下元素一起使用:

  1. <input>
  2. <select>
  3. <textarea>

<input>

我们可以通过以下语法很容易地在 <input> 元素中使用 required 属性:

<input required>

示例

<html>
<head>
<title>Example of required attribute with input element</title>
<style>
div {
  padding: 10px 0;
}
</style>
</head>
<body>
<form>
<div>
<label>Name</label>
<input type="text" placeholder="Enter Name" name="name" required>
</div>
<div>
<label>E-mail</label>
<input type="email" placeholder="Enter email ID" name="email" required>
</div>
<div>
<label>Mobile No.</label>
<input type="text" placeholder="Enter Your Mobile No." name="mobileno" required>
</div>
<div>
<label>Password</label>
<input type="password" placeholder="Enter Password" name="psw" required>
<br>
</div>
<button type="submit" value="SUBMIT">SUBMIT</button>
</form>
</body>
</html>

<select>

我们可以通过以下语法很容易地在 <select> 元素中使用 required 属性:

<select required>

示例

<html>
<head>
<title>Example of required attribute with select option</title>
<style>
div {
  padding: 10px 0;
}
</style>
</head>
<body>
<form>
<label>Course:</label>
<select required>
<option value="">None</option>
<option value="BCA">BCA</option>
<option value="BBA">BBA</option>
<option value="B.Tech">B.Tech</option>
<option value="MBA">MBA</option>
<option value="MCA">MCA</option>
<option value="M.Tech">M.Tech</option>
</select>
<button type="submit" value="SUBMIT">SUBMIT</button>
</form>
</body>
</html>

<textarea>

我们也可以通过以下语法很容易地在 <textarea> 元素中使用 required 属性:

<textarea required>

示例

<html>
<head>
<title>Example of required attribute with textarea</title>
<style>
div {
  padding: 10px 0;
}
</style>
</head>
<body>
<form>
Any Comment:<br>
<textarea cols="80" rows="5" placeholder="Enter a comment" value="address" required></textarea>
<button type="submit" value="SUBMIT">SUBMIT</button>
</form>
</body>
</html>

浏览器支持

元素chrome browser Chromeie browser IEfirefox browser Firefoxopera browser Operasafari browser Safari
<required>

标签: html, HTML教程, HTML技术, HTML学习, HTML学习教程, HTML下载, HTML语言, HTML开发, HTML入门教程, HTML进阶教程, HTML面试题, HTML笔试题, HTML编程思想