I written an application that allows users to enter their personal and payment details.
I have a number of validation checks on the majority of the fields to ensure they are not blank and hope in the future to introduce other checks (e.g. numeric values only, no special characters)..
I look at my jQuery code, and it just seems i have gone about it in the long-winded way, using if else's. Here's my jQuery code using a sample of fields:
$(document).ready(function () {
$(".submitForm").click(function () {
if ($("#Firstname").val() == '') {
$("#Firstname").addClass("highlight");
} else {
$("#Firstname").removeClass("highlight");
}
if ($("#Surname").val() == '') {
$("#Surname").addClass("highlight");
} else {
$("#Surname").removeClass("highlight");
}
if ($("#email").val() == '') {
$("#email").addClass("highlight");
} else {
$("#email").removeClass("highlight");
}
if ($("#confirmemail").val() == '') {
$("#confirmemail").addClass("highlight");
} else {
$("#confirmemail").removeClass("highlight");
}
......
......
$("#PersonalDetailsRequired").removeClass("visuallyhidden ");
//document.forms[0].submit();
});
});
Is there a more efficient way of writing this?
Here's my jFiddle: http://jsfiddle.net/oampz/G3ccb/5/
Also, PersonalDetailsRequired seems to always be appear even if ALL the fields have a value in them.
Thanks
UPDATE:
Uploaded a new fiddle here - http://jsfiddle.net/oampz/P9eKZ/1/
What i'm trying to do is for the #PersonalDetailsRequired and #AddressDetailsRequired to only show if their respective fields are not filled in..
Thanks