# Spot the bug - #98: Submit Form

**URL:** <https://forum.kirupa.com/t/spot-the-bug-98-submit-form/682751>\
**Category:** web dev\
**Created:** [July 26, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-98-submit-form/682751 "2026-07-26T07:00:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [July 26, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-98-submit-form/682751/1 "2026-07-26T07:00:09Z")

</div>

Password form validator silently lets bad input through.

```js
const form = document.querySelector('#signupForm');
const pwInput = document.querySelector('#password');
const confirmInput = document.querySelector('#confirmPassword');
const errorBox = document.querySelector('#formError');

form.addEventListener('submit', (event) => {
  const pw = pwInput.value;
  const confirmPw = confirmInput.value;

  if (pw.length < 8 || pw !== confirmPw) {
    errorBox.textContent = 'Passwords must match and be 8+ characters.';
  }

  console.log('Form submitted with valid password');
});

```

Reply with what is broken and how you would fix it.

---

<div class="post-metadata">

**Author:** ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)\
**Post date:** [July 27, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-98-submit-form/682751/2 "2026-07-27T07:00:22Z")

</div>

Missing `event. preventDefault()` and no `return` after the error, so the form submits and the page reloads even when validation fails. Also that console. log runs on every submit, valid or not, so the log message is just lying to you at that point.

---

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [July 27, 2026, 8:00am UTC](https://forum.kirupa.com/t/spot-the-bug-98-submit-form/682751/3 "2026-07-27T08:00:14Z")

</div>

**Spot the Bug answer:** The submit handler shows the error message but never calls event.preventDefault(), so the form submits regardless of validation failing.

**The fix:**

```js
Add 'event.preventDefault(); return;' inside the if block before the console.log, e.g. if (pw.length < 8 || pw !== confirmPw) { errorBox.textContent = '...'; event.preventDefault(); return; }

```

**Why:**  
Setting errorBox.textContent only updates the DOM, it does not stop the browser’s default form submission behavior. Without calling preventDefault (and returning early), the code falls through to the console.log line and the form still submits even when validation fails.
