title: Regular expression to validate username
body: <p>I'm trying to create a regular expression to validate usernames against these criteria:</p>
  
  <ol>
  <li>Only contains <em>alphanumeric</em> characters,
  <em>underscore</em> and <em>dot</em>.</li>
  <li>Underscore and dot can't be at the <em>end</em> or
  <em>start</em> of a username (e.g <code>_username</code> / <code>username_</code> / <code>.username</code> / <code>username.</code>).</li>
  <li>Underscore and dot can't be <em>next to each other</em> (e.g <code>user_.name</code>).</li>
  <li>Underscore or dot can't be used multiple times <em>in a row</em> (e.g <code>user__name</code> / <code>user..name</code>).</li>
  <li>Number of characters must be between 8 to 20.</li>
  </ol>
  
  <p>This is what I've done so far; it sounds it enforces all criteria rules <em>but the 5th rule</em>. I don't know how to add the 5th rule to this:</p>
  
  <pre><code> ^[a-zA-Z0-9]+([._]?[a-zA-Z0-9]+)*$
  </code></pre>