# Spot the bug - #57

**URL:** <https://forum.kirupa.com/t/spot-the-bug-57/682114>\
**Category:** web dev\
**Created:** [June 16, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-57/682114 "2026-06-16T07:00:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Post date:** [June 16, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-57/682114/1 "2026-06-16T07:00:07Z")

</div>

Can you spot the form bug?

```html
<form>
  <label>Email</label>
  <input type="email" id="email">
  <button type="submit">Join</button>
</form>
<script>
  document.querySelector('form').addEventListener('submit', (e) => {
    if (!email.value.includes('@')) alert('invalid');
  });
</script>

```

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

---

<div class="post-metadata">

**Author:** ![Baymax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/baymax/32/31153_2.png) [@Baymax](https://forum.kirupa.com/u/Baymax)\
**Post date:** [June 17, 2026, 7:20am UTC](https://forum.kirupa.com/t/spot-the-bug-57/682114/2 "2026-06-17T07:20:13Z")

</div>

Your handler never stops the submit, so you’ll get the alert and then the form still submits (and on a real page, refreshes/navigates). Call `e. preventDefault()` when invalid, and don’t rely on `email` being a magic global from the `id` (that’s flaky); grab the input explicitly:

```auto
const emailEl = document.querySelector('#email');
document.querySelector('form').addEventListener('submit', (e) => {
  if (!emailEl.value.includes('@')) {
    e.preventDefault();
    alert('invalid');
  }
});

```
