Thuộc tính bắt buộc trong HTML DOM DOM Input Email quyết định liệu Email nhập liệu có bắt buộc phải đặt hay không.
Cú pháp
Sau đây là cú pháp -
- Trả về giá trị boolean - true / false
inputEmailObject.required
- Cài đặt bắt buộc sang booleanValue
inputEmailObject.required = booleanValue
Giá trị Boolean
Đây, “booleanValue” có thể như sau -
| booleanValue | Chi tiết |
|---|---|
| true | Bắt buộc phải đặt trường email để gửi biểu mẫu. |
| false | Đây là giá trị mặc định và việc đặt trường email là không bắt buộc. |
Ví dụ
Hãy để chúng tôi xem một ví dụ về Yêu cầu Email đầu vào tài sản -
<!DOCTYPE html>
<html>
<head>
<title>Input Email required</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
}
input[type="button"] {
border-radius: 10px;
}
</style>
</head>
<body>
<form>
<h1>Compose Mail</h1>
<fieldset>
<legend>Email-required</legend>
<label for="EmailSelect">Email Id :
<input type="email" id="EmailSelect" required>
</label><br>
<label for="textarea">Message :
<textarea id="textarea" rows="2" cols="20" name="message" placeholder="Type message..."></textarea></label><br>
<input type="button" onclick="showMessage()" value="Send Mail">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var inputEmail = document.getElementById("EmailSelect");
function showMessage() {
if(inputEmail.required === true && inputEmail.value === '')
divDisplay.textContent = 'Cannot Send Mail: Email Id Required';
else
divDisplay.textContent = 'Mail Sent';
}
</script>
</body>
</html> Đầu ra
Điều này sẽ tạo ra kết quả sau -
Nhấp vào ‘Gửi thư’ nút -
Sau khi nhấp vào ‘Gửi thư’ nút có giá trị của một tập trường email -