title: allow keyboard input based on regex or string
body: <p>I am trying to add an <code>allow</code> function for a keydown event. What is supposed to happen, is that you can pass it a <code>RegExp</code> or <code>string</code> of keyboard keys that you want to allow to be inserted into the input field, and everything else then gets rejected.</p>
  
  <p>The issue I am having is with the else. Having it there is blocking every input. Removing the else allows for the <code>RegExp</code> to work, but then strings don't work.</p>
  
  <p>What is causing this?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function allow(evt, ...names) {
    for (let i = 0; i &lt; names.length; i++) {
      let name = names[i]
      if (name instanceof RegExp) {
        if (!name.test(evt.key)) {
          evt &amp;&amp; evt.preventDefault()
        }
      } else {
        name = name.toString()
        if (name.toLowerCase() != evt.key.toLowerCase()) {
          evt &amp;&amp; evt.preventDefault()
        }
      }
    }
  }
  
  document.querySelector('input').addEventListener('keydown', e =&gt; {
    allow(e, /\d/, 'Backspace')
  })</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;input type="text"&gt;</code></pre>
  </div>
  </div>
  </p>