HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavascriptMinor

My form error javascript code

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
javascriptformerrorcode

Problem

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!#$%&'+/=?^_
{|}~-]+(?:\.[a-z0-9!#$%&'+/=?^_`{|}~-]+)@(?:a-z0-9?\.)+a-z0-9?/;
if (val == '') {

$('#' + id).after('Enter your email');
}
else if (reg.test(val) == false) {

$('#' + id).after('Email invalid');
}
else {

errors.email = false;
$('#' + id).after('Email OK');
}
joinAjax('cemail');
}
if (id == 'cemail') {

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

if (val != email) {

$('#' + id).after('Emails do not match');
}
else {

errors.cemail = false;
$('#' + id).after('Success');
}
}
else {

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

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

$('#' + id).after('Enter a password');
}
else if (val.length Minimum length of 6');
}
else {

errors.password = false;
$('#' + id).after('Password OK');
}
joinAjax('cpassword');
}
if (id == 'cpassword') {

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

if (val != password) {

$('#' + id).after('Passwords do not match');
}
else {

errors.cpassword = false;
$('#' + id).after('Success');
}
}
else {

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

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

$('#' + id).after('Enter a username');
}
else if (val.length Minimum length is 3');
}
els

Solution

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

Context

StackExchange Code Review Q#4959, answer score: 3

Revisions (0)

No revisions yet.