//http://yensdesign.com/2009/01/how-validate-forms-both-sides-using-php-jquery/
$(document).ready(function() {
//global vars
var form = $("#customForm");
var orderedBy = $("#orderedBy");
var nameInfo = $("#nameInfo");
var phoneNumber = $("#phoneNumber");
var phoneInfo = $("#phoneInfo");
var emailaddressVal = $("#Name_A").val();
var emailblockReg = /^([\w-\.]+@(?!gmail.com)(?!yahoo.com)(?!hotmail.com)([\w-]+\.)+[\w-]{2,4})?$/;
//On blur
orderedBy.blur(validateName);
phoneNumber.blur(validatePhone);
Name_A.blue(validateEmail);
//On key press
orderedBy.keyup(validateName);
orderedBy.keyup(validatePhone);
Name_A.keyup(validateEmail);
//On Submitting
form.submit(function() {
if (validateName() & validatePhone()) {
return true
} else {
alert('Please check that all required boxes are filled.');
return false;
}
});
//validation functions
function validateName() {
//if it's NOT valid
if (orderedBy.val().length < 1) {
orderedBy.addClass("error");
nameInfo.text("!");
nameInfo.addClass("error");
return false;
}
//if it's valid
else {
orderedBy.removeClass("error");
nameInfo.text("*");
nameInfo.removeClass("error");
return true;
}
}
function validatePhone() {
//if it's NOT valid
if (phoneNumber.val().length < 1) {
phoneNumber.addClass("error");
phoneInfo.text("!");
phoneInfo.addClass("error");
return false;
}
//if it's valid
else {
phoneNumber.removeClass("error");
phoneInfo.text("*");
phoneInfo.removeClass("error");
return true;
}
}
function validateEmail() {
if (!emailblockReg.test(emailaddressVal)) {
alert('Not Today!');
return false;
}
}
});
|