參考:https://bootstrap5.hexschool.com/docs/5.1/forms/validation/
為了自定義 Bootstrap 表單驗證訊息,您需要將布林屬性 novalidate
添加到 <form>
中。這將會禁用瀏覽器預設的回饋工具提示,不過 JavaScript 的表單驗證 API 依然有效。嘗試提交以下的表單,Bootstrap 的 JavaScript 將會攔截提交按鈕並向您提供回饋。在嘗試提交時,您會看到 :invalid
和 :valid
的樣式被應用於您的表單控制元件。
但仍提供 JavaScript 中表單驗證 API 有效。嘗試提交以下表格;我們的 JavaScript 將攔截提交按鈕並向您提供回饋。嘗試提交時,您會看到 :invalid
和 :valid
的樣式應用於表單控制元件 (form control)。
自定義的回饋樣式將會採用客製化的顏色、邊界、focus 樣式和背景圖示以更好的傳遞回饋。<select>
的背景圖示僅適用於 .form-select
而不可使用於 .form-control
。
<form class="row g-3 needs-validation" novalidate>
<div class="col-md-4">
<label for="validationCustom01" class="form-label">First name</label>
<input type="text" class="form-control" id="validationCustom01" value="Mark" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-4">
<label for="validationCustom02" class="form-label">Last name</label>
<input type="text" class="form-control" id="validationCustom02" value="Otto" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-4">
<label for="validationCustomUsername" class="form-label">Username</label>
<div class="input-group has-validation">
<span class="input-group-text" id="inputGroupPrepend">@</span>
<input type="text" class="form-control" id="validationCustomUsername" aria-describedby="inputGroupPrepend" required>
<div class="invalid-feedback">
Please choose a username.
</div>
</div>
</div>
<div class="col-md-6">
<label for="validationCustom03" class="form-label">City</label>
<input type="text" class="form-control" id="validationCustom03" required>
<div class="invalid-feedback">
Please provide a valid city.
</div>
</div>
<div class="col-md-3">
<label for="validationCustom04" class="form-label">State</label>
<select class="form-select" id="validationCustom04" required>
<option selected disabled value="">Choose...</option>
<option>...</option>
</select>
<div class="invalid-feedback">
Please select a valid state.
</div>
</div>
<div class="col-md-3">
<label for="validationCustom05" class="form-label">Zip</label>
<input type="text" class="form-control" id="validationCustom05" required>
<div class="invalid-feedback">
Please provide a valid zip.
</div>
</div>
<div class="col-12">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
<label class="form-check-label" for="invalidCheck">
Agree to terms and conditions
</label>
<div class="invalid-feedback">
You must agree before submitting.
</div>
</div>
</div>
<div class="col-12">
<button class="btn btn-primary" type="submit">Submit form</button>
</div>
</form>
JS
<script>
// Example starter JavaScript for disabling form submissions if there are invalid fields
(function() {
'use strict'
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.querySelectorAll('.needs-validation')
// Loop over them and prevent submission
Array.prototype.slice.call(forms)
.forEach(function(form) {
form.addEventListener('submit', function(event) {
if (!form.checkValidity()) {
event.preventDefault()
event.stopPropagation()
}
form.classList.add('was-validated')
}, false)
})
})()
</script>