My form error javascript code Simple really my javascript function for checking my form.

var errors = {};
errors.email = true;
errors.cemail = true;
errors.password = true;
errors.cpassword = true;
errors.username = true;
function joinAjax (id) {

var val = $('#' + id).val();
if (id == 'email') {

    $('#emailMsg').hide();
    var reg = /[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
    if (val == '') {

        $('#' + id).after('&lt;div id="emailMsg" class="error"&gt;Enter your email&lt;/div&gt;');
    }
    else if (reg.test(val) == false) {

        $('#' + id).after('&lt;div id="emailMsg" class="error"&gt;Email invalid&lt;/div&gt;');
    }
    else {

        errors.email = false;
        $('#' + id).after('&lt;div id="emailMsg" class="success"&gt;Email OK&lt;/div&gt;');
    }
    joinAjax('cemail');
}
if (id == 'cemail') {

    $('#cemailMsg').hide();
var email = $('#email').val();
    if (errors.email == false) {

        if (val != email) {

            $('#' + id).after('&lt;div id="cemailMsg" class="error"&gt;Emails do not match&lt;/div&gt;');
        }
        else {

            errors.cemail = false;
            $('#' + id).after('&lt;div id="cemailMsg" class="success"&gt;Success&lt;/div&gt;');
        }
    }
    else {

        $('#cemail').val('');
    }
}
if (id == 'password') {

    $('#passwordMsg').hide();
    if (val == '') {

        $('#' + id).after('&lt;div id="passwordMsg" class="error"&gt;Enter a password&lt;/div&gt;');
    }
    else if (val.length &lt; 6) {

        $('#' + id).after('&lt;div id="passwordMsg" class="error"&gt;Minimum length of 6&lt;/div&gt;');
    }
    else {

        errors.password = false;
        $('#' + id).after('&lt;div id="passwordMsg" class="success"&gt;Password OK&lt;/div&gt;');
    }
    joinAjax('cpassword');
}
if (id == 'cpassword') {

    $('#cpasswordMsg').hide();
    var password = $('#password').val();
    if (errors.password == false) {

        if (val != password) {

            $('#' + id).after('&lt;div id="cpasswordMsg" class="error"&gt;Passwords do not match&lt;/div&gt;');
        }
        else {

            errors.cpassword = false;
            $('#' + id).after('&lt;div id="cpasswordMsg" class="success"&gt;Success&lt;/div&gt;');
        }
    }
    else {

        $('#cpassword').val('');
    }        
}
if (id == 'username') {

    $('#usernameMsg').hide();
    if (val == '') {

        $('#' + id).after('&lt;div id="usernameMsg" class="error"&gt;Enter a username&lt;/div&gt;');
    }
    else if (val.length &lt; 3) {

        $('#' + id).after('&lt;div id="usernameMsg" class="error"&gt;Minimum length is 3&lt;/div&gt;');
    }
    else {

        errors.username = false;
        $('#' + id).after('&lt;div id="usernameMsg" class="success"&gt;Available&lt;/div&gt;');
    }
}
$('#joinForm').submit(function(event){

    if ((errors.email == true) || (errors.cemail == true) || (errors.password == true) || (errors.cpassword == true) || (errors.username == true)) {

        event.preventDefault();
    }
    return true;
});
}


Can this be improved? I was looking for a way to create a function for the submit event, but I can't figure it how as I don't know how many parameters I will need each time.
Some tips:


you'd probably better have a look at some jquery validation plugin that would do what you are looking for out of the box
to solve your "unknown parameters", you could probably use the jquery .serialize() function and loop through the array