Regular expression in Javascript validation
<p>I'm very new to javascript and have been playing around with regular expressions.  I'm trying to create a simple password/username validation form.  I believe my regular expressions are wrong and somewhere I messed up with my syntax because this won't run. Could anyone tell me what I did wrong?  Thanks.</p>

<p>Here is my code:</p>

<pre><code> &lt;html&gt;

 &lt;head&gt;

 &lt;title&gt;Username/Password Validation&lt;/title&gt;

 &lt;script type="text/javascript"&gt;

 function check(username, password, passwordAgain)
 {
// at least one number, one lowercase and one uppercase letter 
// at least six - ten characters that are letters, numbers or the underscore 
var pwd = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{6,10}$/;

// at least six - ten characters that are letters, numbers or the underscore 
//the first character CANNOT start with a digit
var user = /^!=.*\d\w{6,10}$/;

if (pwd.test(password)) &amp;&amp; (user.test(username)) &amp;&amp; (password == passwordAgain)
{
alert("Passed Validation");
}
else if (password != passwordAgain)
{
alert(Your passswords don't match.");
}
else if !(pwd.test(password)) &amp;&amp; !(user.test(username))
{
alert(username + password + " Your password and username are not valid.");
}
else if !(pwd.test(password)) &amp;&amp; (user.test(username))
{
alert(password + " This password is not valid.");
}
else
{
alert(username +" This username is not valid.");
}


 &lt;/script&gt;
 &lt;/head&gt;

 &lt;body&gt;

 &lt;form&gt;

 &lt;h3&gt; Username-Password Validation &lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt; The username must be between 6 and 10 characters long. &lt;/li&gt;
&lt;li&gt; The username must contain only letters and digits. &lt;/li&gt;
&lt;li&gt; The username cannot begin with a digit. &lt;/li&gt;
&lt;li&gt; The password and the repeat password must match. &lt;/li&gt;
&lt;li&gt; The password must be between 6 and 10 characters. &lt;/li&gt;
&lt;li&gt; The password must contain only letters and digits. &lt;/li&gt;
&lt;li&gt; The password must have at least one lower case letter,&lt;br /&gt; at
 least one upper case letter, and at least one digit. &lt;/li&gt;
&lt;/ul&gt;


Username: &lt;input type = 'text' id = 'username' /&gt; &lt;br /&gt;

Password: &lt;input type = 'text' id = 'password' /&gt; &lt;br /&gt;

Reenter Password: &lt;input type = 'text' id = 'passwordAgain' /&gt; &lt;br/&gt; 

&lt;p&gt;

&lt;input type ='button' onclick='check(username, password, passwordAgain )' value='Submit' /&gt;

&lt;input type ='reset' value='Clear' /&gt; 

&lt;/p&gt;

&lt;/form&gt;

&lt;/body&gt;

&lt;/html&gt;
</code></pre>

<p><em><strong></em>*EDIT***</strong></p>

<pre><code>&lt;html&gt;
&lt;head&gt;
 &lt;title&gt;Username/Password Validation&lt;/title&gt;

 &lt;script type="text/javascript"&gt;
    function check(username, password, passwordAgain){
        //List of possible error messages
        var errorList = '';

        // at least six - ten characters that are letters, numbers or the underscore
        var sixtotencharacters = /\w{6,10}$/;

        // at least one number, one lowercase and one uppercase letter 
        var oneofeach = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/;

        //first character does not start with digit
        var firstDigit = /^!=.*\d/;

        //Begin validation

        if (sixtotencharacters.test(password) &amp;&amp; sixtotencharacters.test(username)  &amp;&amp; oneofeach.test(password) &amp;&amp; firstDigit.test(username) &amp;&amp; password == passwordAgain)
           {
            alert("Passed Validation");
    }
        else
        {
         if (password != passwordAgain){
            errorlist = 'Your passswords don\'t match. ';
        }if (!sixtotencharacters.test(username)){
            errorlist = errorlist + 'Username needs to be six to ten characters. ';
        }if (!sixtotencharacters.test(password)){
            errorlist = errorlist + 'Password needs to be six to ten characters. ';
        }if (!oneofeach.test(password)){
            errorlist = errorlist + 'You need to have at least one number, one lowercase and one uppercase letter. ';
        }if (!firstDigit.test(username)){
            errorlist = errorlist + 'First character of username can\'t be a digit. ';
        }
    alert(errorlist);

    }
   &lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;form&gt;

&lt;h3&gt; Username-Password Validation &lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt; The username must be between 6 and 10 characters long. &lt;/li&gt;
&lt;li&gt; The username must contain only letters and digits. &lt;/li&gt;
&lt;li&gt; The username cannot begin with a digit. &lt;/li&gt;
&lt;li&gt; The password and the repeat password must match. &lt;/li&gt;
&lt;li&gt; The password must be between 6 and 10 characters. &lt;/li&gt;
&lt;li&gt; The password must contain only letters and digits. &lt;/li&gt;
&lt;li&gt; The password must have at least one lower case letter,&lt;br /&gt; at
 least one upper case letter, and at least one digit. &lt;/li&gt;
&lt;/ul&gt;


Username: &lt;input type = 'text' id = 'username' /&gt; &lt;br /&gt;

Password: &lt;input type = 'text' id = 'password' /&gt; &lt;br /&gt;

Reenter Password: &lt;input type = 'text' id = 'passwordAgain' /&gt; &lt;br/&gt; 

&lt;p&gt;

&lt;input type ='button' onclick='check(username, password, passwordAgain )' value='Submit' /&gt;

&lt;input type ='reset' value='Clear' /&gt; 

&lt;/p&gt;

&lt;/form&gt;

&lt;/body&gt;

&lt;/html&gt;
</code></pre>

<p><em><strong></em>**</strong><em>This is the most recent update that takes Jason's suggestions a step further</em><strong><em>*</em></strong>
       
       
       Username/Password Validation</p>

<pre><code>  &lt;script&gt;
    window.onload = check() {
        document.getElementById('btnSubmit').addEventListener('click', check() {
            var errors = [];
            var username = document.getElementById('username').value;
            var password = document.getElementById('password').value;
    var passwordAgain = document.getElementById('passwordAgain').value;
            var errorList = document.getElementById("errors");

            errorList.innerHTML = '';

    if (password != passwordAgain) { errors.push("Your passwords do not match") }

            var hasNumber = password.match(/\d+/);

            if (hasNumber == null) { errors.push("You did not include a number in your password") }

            var hasUpper = password.match(/[A-Z]+/);

            if (hasUpper == null) { errors.push("You did not include an Uppercase Letter in your password") }

            var hasLower = password.match(/[a-z]+/);

            if (hasLower == null) { errors.push("You did not include an Lowercase Letter in your password") }

    var hasAtleast6lessThan10password = password.length &lt; 6 || password.length &gt; 10; 

    if (hasAtleast6lessThan10password) { errors.push("Your password must be at least 6 characters long and cannot be more than 10") } 

            var hasAtleast6lessThan10username = username.length &lt; 6 || username.length &gt; 10;

            if (hasAtleast6lessThan10username) { errors.push("Your username must be at least 6 characters long and cannot be more than 10") } 

            var firstCharacterNotDigit = username.match(/^[^\d]/);

            if (firstCharacterNotDigit == null) { errors.push("Your username may not begin with a number") }  

            if (errors.length &gt; 0) {

                for (var i = 0; i &lt; errors.length; i++) {
                    var errorElem = document.createElement("li");
                    errorElem.innerHTML = errors[i];

                    errorList.appendChild(errorElem);
                }
            }

        });
    };
&lt;/script&gt;


&lt;/head&gt;

&lt;body&gt;

&lt;form&gt;

&lt;h3&gt; Username-Password Validation &lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt; The username must be between 6 and 10 characters long. &lt;/li&gt;
&lt;li&gt; The username must contain only letters and digits. &lt;/li&gt;
&lt;li&gt; The username cannot begin with a digit. &lt;/li&gt;
&lt;li&gt; The password and the repeat password must match. &lt;/li&gt;
&lt;li&gt; The password must be between 6 and 10 characters. &lt;/li&gt;
&lt;li&gt; The password must contain only letters and digits. &lt;/li&gt;
&lt;li&gt; The password must have at least one lower case letter,&lt;br /&gt; at
 least one upper case letter, and at least one digit. &lt;/li&gt;
&lt;/ul&gt;


Username: &lt;input type = 'text' id = 'username' size="10" /&gt; &lt;br /&gt;

Password: &lt;input type = 'text' id = 'password' size="10"/&gt; &lt;br /&gt;

Reenter Password: &lt;input type = 'text' id = 'passwordAgain' size="10" /&gt; &lt;br/&gt; 

&lt;p&gt;

&lt;input type ='button' onclick='check()' value='Submit' /&gt;

&lt;input type ='reset' value='Clear' /&gt; 

&lt;/p&gt;

&lt;/form&gt;

&lt;/body&gt;

&lt;/html&gt;
</code></pre>
