title: Can I use parameters in form validation?
body: <p>I'm trying to implement a simple form validation, but I can't make it work with parameters somehow.</p>
  <p>This is the code for just one input:</p>
  <pre class="lang-js prettyprint-override"><code>const firstName = document.getElementById(&quot;first-name&quot;);
  const nameRegex = /^[a-zA-Z]{2,15}$/;
  firstName.addEventListener(&quot;blur&quot;, validate(firstName));
  
  function validate(id) {
      let re = &quot;&quot;;
      switch (id) {
          case firstName:
              re = nameRegex;
              break;
          case lastName:
              re = nameRegex;
              break;
          case email:
              re = emailRegex;
              break;
          case password:
              re = passwordRegex;
              break;
      }
  
      if (!re.test(id.value)) {
          id.parentElement.classList.add(&quot;error&quot;);
          id.parentElement.nextElementSibling.classList.remove(&quot;hide&quot;);
      } else {
          id.parentElement.classList.remove(&quot;error&quot;);
          id.parentElement.nextElementSibling.classList.add(&quot;hide&quot;);
      }
  }
  </code></pre>
  <p>It looks like the code can't tap into the id.value field.</p>
  <p>For reference this code works fine:</p>
  <pre class="lang-js prettyprint-override"><code>function validateFirstName() {
      if (!nameRegex.test(firstName.value)) {
          firstName.parentElement.classList.add(&quot;error&quot;);
          firstName.parentElement.nextElementSibling.classList.remove(&quot;hide&quot;);
      } else {
          firstName.parentElement.classList.remove(&quot;error&quot;);
          firstName.parentElement.nextElementSibling.classList.add(&quot;hide&quot;);
      }
  }
  </code></pre>
  <p>Can anyone point me in the right direction? (I only want to use vanilla JS)</p>