Bootstrap 5: การตรวจสอบแบบฟอร์ม


สารบัญ

    แสดงสารบัญ

การตรวจสอบแบบฟอร์ม

Valid.
Please fill out this field.
Valid.
Please fill out this field.
Valid.
Check this checkbox to continue.

คุณสามารถใช้คลาสการตรวจสอบที่แตกต่างกันเพื่อให้ข้อเสนอแนะอันมีค่าแก่ผู้ใช้ เพิ่ม .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">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>

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>

<div class="container mt-3">
  <h3>Form Validation</h3>
  <p>Try to submit the form.</p>
    
  <form action="/action_page.php" 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>
</div>

</body>
</html>