I got my form to work using the jquery validation plugin! Thanks to Praveen for suggesting it. It's a great tool.
The underlying issue was that I really wasn't wrapping my head around .load(). Once I utilized the .load() callback function, the validation ran fine. I created an external javascript file called contact.js which contains the validation plugin rules and submitHandler nested within contactPageScript() function. I called contactPageScript() with the .load() callback and voila:
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script type="text/javascript" src="js/contact.js"></script>
<script>
$(document).ready(function(){
$("div#menu ul li a").click(function() {
var whichPage = $(this).attr('href');
$('div#page_content').load(($(this).attr('href') + ' #contentB'), function(){
if (whichPage == 'contact.html') {
contactPageScript();
}}
);
});
});
</script>
</head>
The validation script of course also worked fine nested within the callback function rather than as an external .js, but it was too lengthy for my tastes. And there were some small details to be worked out regarding the validation rules. Here's the chunk of validation code I used just for reference:
$(document).ready(function() {
$('#mailform').validate( {
rules: {
fullname: {
required: true,
minlength:2
},
email: {
required: true,
email: true
}
},
messages: {
fullname: 'Please enter a valid name',
email: 'Please enter a valid email address'
},
submitHandler: function(form) {
$.post('php/submitForm.php', $('#mailform').serialize(), function() { DO STUFF });
}
});
});
At first I couldn't get the plugin script to work during debugging. Beware of extra commas after the rules! And also be advised that some copypasta tutorials contain hidden invalid characters within them! You'd never know! I had to rewrite the whole code by hand to get it to validate!
I later added nested if/else statements within the callback to call functions for all the other pages on the site which required jquery/javascript. At first I tried using a switch instead of nested if/elses, but it was not ideal. It helps to read the manual!
Cheers.