I am fairly new to jQuery so my code probably is not the most streamlined. I am having a go at writing my own form validation script which is all working fine.
But in an effort to cut down on CSS I want to display error messages for each form input in the same div tag. So the div tag of valid_prompt is placed after every input field.
The problem I am currently having is all the error messages are being displayed in the first appearance of the div tag, which is expected, but I need some way of linking each unique input field to the div which displays the errors.
Form input Example
<input name="email1" id="email1" type="text" size="30" maxlength="80"
value="<?php echo "$email1"; ?>" /><span id="valid_prompt"></span>
<br/>
<br/>
<label><span class="reqfieldstar"> * </span>Confirm E-mail Address:</label>
<input name="email2" id="email2" type="text" size="30" maxlength="40" value="<?php echo "$email2"; ?>" /><span id="valid_prompt"></span>
Jquery Code
var form = $("#SignupForm"); // Id of Form
var email = $("#email1"); // Email Address ID
var email_compare = $("#email2"); // Compare Email Address ID
var valid_prompt = $("#valid_prompt");
var lettersReg = /^([a-zA-Z,-]|\s)*$/;
var emailReg = /^(\S+@\S+(\.\S+)*\.(\S{2,4}))?$/i;
email.keyup(validateEmail);
email_compare.keyup(validateEmail_Compare);
firstname.keyup(validateFirstname);
surname.keyup(validateSurname);
email.blur(validateEmail);
email_compare.blur(validateEmail_Compare);
firstname.blur(validateFirstname);
surname.blur(validateSurname);
form.submit(function() {
if (validateEmail() && validateEmail_Compare() && validateFirstname() && validateSurname()) {
return true;
} else {
return false;
}
});
function validateEmail() {
if (email.val().length == 0) {
email.addClass("error");
valid_prompt.removeClass("correct");
valid_prompt.text("You Must Enter Your E-mail Address");
valid_prompt.removeClass("correct");
valid_prompt.addClass("error");
return false;
} else {
if (!emailReg.test(email.val())) {
email.addClass("error");
valid_prompt.removeClass("correct");
valid_prompt.text("Please Enter a Valid E-mail Address");
valid_prompt.removeClass("correct");
valid_prompt.addClass("error");
return false;
} else {
email.removeClass("error");
valid_prompt.text("");
valid_prompt.addClass("correct");
valid_prompt.removeClass("error");
return true;
}
}
} // Email Function End
function validateEmail_Compare() {
if (email_compare.val().length == 0) {
email_compare.addClass("error");
valid_prompt.removeClass("correct");
valid_prompt.text("You Must Confirm Your E-mail Address");
valid_prompt.removeClass("correct");
valid_prompt.addClass("error");
return false;
} else {
if (!emailReg.test(email_compare.val())) {
email_compare.addClass("error");
valid_prompt.removeClass("correct");
valid_prompt.text("Please Enter a Valid E-mail Address");
valid_prompt.removeClass("correct");
valid_prompt.addClass("error");
return false;
} else {
if (email.val() != email_compare.val()) {
email_compare.addClass("error");
valid_prompt.removeClass("correct");
valid_prompt.text("Your E-mail Addresses Do Not Match");
valid_prompt.removeClass("correct");
valid_prompt.addClass("error");
return false;
} else {
email_compare.removeClass("error");
valid_prompt.text("");
valid_prompt.addClass("correct");
valid_prompt.removeClass("error");
return true;
}
}
}
} // Email Compare Function End