Hello I'm quite new to JQuery & I'm having trouble getting the validation to work proper with a contact form tutorial i followed that did not include a select box.
Here is the JQuery code:
$(".txtbar, .txtbox").live("focus", function () {
var thelabel = $(this).prev();
var infobox = $(this).next();
var rowbox = $(this).parent();
var currid = $(this).attr('id');
var pxlchange = '-145px';
if (currid == "contactnumber") {
pxlchange = '-145px';
}
if (currid == "message") {
pxlchange = '-145px';
}
rowbox.addClass('colors');
thelabel.animate({
left: pxlchange
}, 350, 'linear', function () {
// animation complete
});
infobox.animate({
opacity: 1.0
}, 350, 'linear', function () {
// animation complete
});
$(this).live("keyup", function () {
var theval = $(this).val();
var value = $("subject").val();
var limitval = 3;
var replacehtml = "";
var nameinfohtml = "Not a valid Name!";
var emailinfohtml = "Not a valid E-mail address!";
var contactnumberinfohtml = "This field is optional!";
var subjectinfohtml = "Not a valid subject!";
var messageinfohtml = "Please fill this field out!";
if (currid == "name") {
replacehtml = nameinfohtml;
} else if (currid == "email") {
replacehtml = emailinfohtml;
} else if (currid == "contactnumber") {
replacehtml = contactnumberinfohtml;
limitval = 9;
} else if (value == '0') {
replacehtml = subjectinfohtml;
} else if (currid == "message") {
replacehtml = messageinfohtml;
limitval = 10;
}
// checking against e-mail regex
if (currid == "email") {
if (checkValidEmailAddress(theval)) {
infobox.html("Accepted!");
infobox.addClass('good');
} else if (!checkValidEmailAddress(theval)) {
infobox.html(replacehtml);
infobox.removeClass('good');
}
} else {
// we use this logic to check for name+message fields
if (theval.length >= limitval) {
infobox.html("Accepted!");
infobox.addClass('good');
} else if (theval.length < limitval) {
infobox.html(replacehtml);
infobox.removeClass('good');
}
// we use this logic to check the subject
if (value.val() !== '0') {
infobox.html("Accepted!");
infobox.addClass('good');
} else if (value.val() == '0') {
infobox.html(replacehtml);
infobox.removeClass('good');
}
}
// now we check if we can display the send button
// much easier to just look for 'good class on the req fields
if ($('#name').next().hasClass('good') && $('#email').next().hasClass('good') && $('#message').next().hasClass('good')) {
// if all 3 boxes are good then we display our send button!
// jquery validation complete
$('#sendbtn').animate({
opacity: 1.0
}, 200, 'linear', function () {
// display submitbtn animation complete
});
}
});
});
$(".txtbar, .txtbox").live("blur", function () {
var thelabel = $(this).prev();
var infobox = $(this).next();
var rowbox = $(this).parent();
var currid = $(this).attr('id');
rowbox.removeClass('colors');
infobox.animate({
opacity: 0
}, 400, 'linear', function () {
// animation complete
});
});
Here is my JSfiddle attempt at making it work.
Basically im just trying to get it to find the value "0" and add a class of "good" so it will read the accept message when u pick a subject, I have read a lot of ways how to do this but im not quite sure how to incorporate it this script.
thank you for your time all help is very much appreciated.
valuenever be used correctly . theAcceptedadd to select is handled by this codeif (theval.length >= limitval) { infobox.html("Accepted!"); infobox.addClass('good'); }