您可以使用不同的验证类向用户提供有值的反馈。添加任一.was-validated
或者.needs-validation
到<form>
元素,具体取决于您是否想在提交表单之前或之后提供验证反馈。输入字段将具有绿色(有效)或红色(无效)边框来指示表单中缺少的内容。您还可以添加一个.valid-feedback
或者.invalid-feedback
消息明确告诉用户缺少什么,或者在提交表单之前需要完成什么。
在这个例子中,我们使用.was-validated
在提交表格之前指出缺少的内容:
<form action="/action_page.html" class="was-validated">
<div class="mb-3 mt-3">
<label for="uname" class="form-label">Username:</label>
<input type="text" class="form-control" id="uname" placeholder="Enter username" name="uname" required>
<div class="valid-feedback">Valid.</div>
<div class="invalid-feedback">Please fill out this field.</div>
</div>
<div class="mb-3">
<label for="pwd" class="form-label">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pswd" required>
<div class="valid-feedback">Valid.</div>
<div class="invalid-feedback">Please fill out this field.</div>
</div>
<div class="form-check mb-3">
<input class="form-check-input" type="checkbox" id="myCheck" name="remember" required>
<label class="form-check-label" for="myCheck">I agree on blabla.</label>
<div class="valid-feedback">Valid.</div>
<div class="invalid-feedback">Check this checkbox to continue.</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
亲自试一试 »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!