0
0
Bootsrapmarkup~20 mins

Form validation styles in Bootsrap - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bootstrap Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
What is the output of this Bootstrap form validation code?
Consider this HTML snippet using Bootstrap 5 validation classes. What will the user see after submitting the form with an empty required input?
Bootsrap
<form class="needs-validation" novalidate>
  <div class="mb-3">
    <label for="emailInput" class="form-label">Email address</label>
    <input type="email" class="form-control" id="emailInput" required>
    <div class="invalid-feedback">
      Please provide a valid email.
    </div>
  </div>
  <button class="btn btn-primary" type="submit">Submit</button>
</form>
<script>
(() => {
  'use strict'
  const form = document.querySelector('.needs-validation')
  form.addEventListener('submit', event => {
    if (!form.checkValidity()) {
      event.preventDefault()
      event.stopPropagation()
    }
    form.classList.add('was-validated')
  }, false)
})()
AThe input border turns red and the message 'Please provide a valid email.' appears below the input.
BThe form submits successfully without any validation message.
CThe input border turns green and the message 'Please provide a valid email.' appears.
DNothing happens; the form submits and reloads the page.
Attempts:
2 left
💡 Hint
Bootstrap uses 'was-validated' class to show validation feedback after checking validity.
selector
intermediate
1:30remaining
Which CSS selector applies styles to invalid form inputs in Bootstrap?
Bootstrap uses CSS selectors to style invalid inputs. Which selector correctly targets invalid inputs to show red borders?
Ainput.invalid
B.was-validated .form-control:invalid
C.form-control:invalid
Dinput:invalid
Attempts:
2 left
💡 Hint
Bootstrap applies validation styles only after the form has the 'was-validated' class.
rendering
advanced
1:30remaining
What visual result will this Bootstrap validation produce?
This form input has the class 'form-control is-valid' applied. What will the user see?
Bootsrap
<input type="text" class="form-control is-valid" aria-describedby="validFeedback">
<div id="validFeedback" class="valid-feedback">
  Looks good!
</div>
AThe input border is green but the message is hidden.
BThe input border is red and the message 'Looks good!' is visible.
CThe input border is default gray and no message is shown.
DThe input border is green and the message 'Looks good!' is visible below the input.
Attempts:
2 left
💡 Hint
The 'is-valid' class triggers green border and shows the valid-feedback message.
accessibility
advanced
1:30remaining
Which attribute improves accessibility for Bootstrap validation feedback?
To help screen readers associate validation messages with inputs, which attribute should be used on the input element?
Aaria-hidden
Baria-label
Caria-describedby
Daria-live
Attempts:
2 left
💡 Hint
This attribute links the input to the feedback message by ID.
🧠 Conceptual
expert
2:00remaining
How does Bootstrap's validation script prevent form submission on invalid input?
Examine this JavaScript snippet used with Bootstrap validation. What is the key method that stops the form from submitting if inputs are invalid?
Bootsrap
form.addEventListener('submit', event => {
  if (!form.checkValidity()) {
    event.preventDefault()
    event.stopPropagation()
  }
  form.classList.add('was-validated')
}, false)
Aevent.preventDefault() stops the form submission when inputs are invalid.
Bevent.stopPropagation() alone stops the form submission.
Cform.classList.add('was-validated') prevents submission automatically.
DcheckValidity() submits the form if inputs are valid.
Attempts:
2 left
💡 Hint
Preventing default behavior stops the form from sending data.