How to restrict the same character from being used consecutively?
<p>I am trying to create a username input and I want to restrict the user's input onkeypress. The limit of characters that can be entered is 20 + 1 being the fixed prefix.</p>

<p>What I want to achieve is:</p>

<ol>
<li>To restrict user input and accept only A-Z, 0-9, dash (-) and underscore (_). [Solved]</li>
<li>To prohibit the user to start their username with dash (-) or underscore (_). [Solved]</li>
<li>To prohibit the user to use dash (-) if they have used underscore (_) and the opposite. [Solved]</li>
<li>To allow the user to use a max of 3 dashes (-) or 3 underscores (_). [Dashes Solved]</li>
<li>To prohibit the user to use consecutive symbols like so (user___ or user--p, etc) [NEED HELP!]</li>
</ol>

<p>What I can't seem to figure out is how to restrict the underscore (_) to 3 max and how to stop consecutive dashes (-) and underscores (_).</p>

<p>Any help and proper explanation would be greatly appreciated!</p>

<p>My HTML:</p>

<pre><code>&lt;form name = "RegForm" method="post" action="index.html" class="login"&gt;
    &lt;input type="text" maxlength = "21" name="userID" id="userID"/&gt;
&lt;/form&gt;
</code></pre>

<p>My JavaScript:</p>

<pre><code>var userID_textfield = document.forms.RegForm.userID;
userID_textfield.onkeypress = function(e) {

    // Invalid character list
    var prohibited = "!@#$%^&amp;*()+=;:`~\|'?/.&gt;&lt;, \"";
    // List of characters that can't be first
    var posRestricted = "-_0123456789";
    // List of characters that can't be used more than once
    var numRestricted = "-_";
    // Get the actual character string value
    var key = String.fromCharCode(e.which);

    /* Goal:
       Validate: Accept only a-z, 0-9, - and _ */  
    if (prohibited.indexOf(key) &gt;= 0) {
        console.log('Invalid key pressed');     
        return false;
    }
    else {
        /* Goals:
           1. Validate: -, _ and 0-9 can't be first
           2. Validate: - and _ can't be last if the userID is 21 characters long */
        if ((posRestricted.indexOf(key) &gt;= 0 &amp;&amp; this.value === "@") || (numRestricted.indexOf(key) &gt;= 0 &amp;&amp; this.value.length === 20)) {
            console.log("Username can't start with a number, a dash (-) or an underscore (_)");
            return false;
        }
        /* Goals:
           1. Validate: - and _ can't be used more than once each
           2. Validate: if - exists _ can't be used and the opposite' */
        else if (numRestricted.indexOf(key) &gt;= 0) {
            var numResValue = [0, 0];
            for (var a = 0; a &lt; numRestricted.length; a++) {
                for (var b = 0; b &lt; this.value.length; b++) {
                    if (this.value.charAt(b) === numRestricted[a]) {
                       numResValue[a] += 1;
                    }
                }
            }
            for (var c = 0; c &lt;= numResValue.length; c++) {
                if (numResValue[c] &lt; 3) {
                    if (this.value.indexOf(numRestricted.replace(key, "")) === -1) {
                        return true;
                    }
                    else {
                        return false;
                    }
                }
                else {
                     return false;
                }
            }
        }
        else {
            return true;
        }
    } 
};
</code></pre>

<p>You can view the code in action <a href="https://jsfiddle.net/ck7f9t6x/3/" rel="nofollow">here</a>.</p>