Thuộc tính required của đối tượng Input URL trong HTML DOM xác định ô nhập URL có bắt buộc phải điền trước khi submit form hay không.
Cú pháp
Lấy giá trị boolean
inputURLObject.required
Thiết lập required
inputURLObject.required = booleanValue
Các giá trị boolean
| booleanValue | Chi tiết |
|---|---|
| True | Bắt buộc nhập URL để submit form. |
| False | Mặc định: không bắt buộc. |
Ví dụ minh họa
Ô URL có required=true. Kiểm tra khi nhấn "Go":
<!DOCTYPE html>
<html>
<head>
<title>Input URL 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>
<fieldset>
<legend>URL-required</legend>
<label for="URLSelect">URL: <input type="url" id="URLSelect" required> </label><br>
<input type="button" onclick="hienThongBao()" value="Go">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var inputURL = document.getElementById("URLSelect");
function hienThongBao() {
if(inputURL.required === true && inputURL.value === '')
divDisplay.textContent = 'Không thể chuyển hướng: URL là bắt buộc';
else
divDisplay.textContent = 'Đang chuyển hướng...';
}
</script>
</body>
</html>
Kết quả
- Nhấn Go khi ô trống: "Không thể chuyển hướng: URL là bắt buộc".
- Nhập URL, nhấn Go: "Đang chuyển hướng...".
Lưu ý: Validation HTML5 tự động chặn submit nếu type="submit" và required=true. Ví dụ dùng type="button" cần kiểm tra thủ công.