Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Bootstrap 5 表单验证


表单验证

有效。
请填写此字段。
有效。
请填写此字段。
有效。
请勾选此复选框以继续。

您可以使用不同的验证类为用户提供有价值的反馈。将 .was-validated.needs-validation 添加到 <form> 元素中,具体取决于您是在提交表单之前还是之后提供验证反馈。输入字段将具有绿色(有效)或红色(无效)边框,以指示表单中缺少什么。您还可以添加 .valid-feedback.invalid-feedback 消息,以明确告诉用户在提交表单之前缺少什么或需要做什么。

示例

在此示例中,我们使用 .was-validated 在提交表单之前指示缺少的内容

<form action="/action_page.php" class="was-validated">
  <div class="mb-3 mt-3">
    <label for="uname" class="form-label">用户名:</label>
    <input type="text" class="form-control" id="uname" placeholder="输入用户名" name="uname" required>
    <div class="valid-feedback">有效.</div>
    <div class="invalid-feedback">请填写此字段.</div>
  </div>
  <div class="mb-3">
    <label for="pwd" class="form-label">密码:</label>
    <input type="password" class="form-control" id="pwd" placeholder="输入密码" name="pswd" required>
    <div class="valid-feedback">有效.</div>
    <div class="invalid-feedback">请填写此字段.</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">我同意 blabla.</label>
    <div class="valid-feedback">有效.</div>
    <div class="invalid-feedback">请勾选此复选框以继续.</div>
  </div>
  <button type="submit" class="btn btn-primary">提交</button>
</form>
自己试试 »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.