Client-side validation is always going to fall second-best to server-side validation, as the user can always disable Javascript or force-post nonsense that can override browser ability, but your server can't be fooled as easily! That said, you're on the right track, although I'd be tempted to fix it up a little...first, your HTML (abridged):
<form method="post">
<input id="ship_to_billing" type="checkbox" name="properties" />
<div class="fieldgroup" id="shipping_info">
<input type="text" id="shipping_name" name="properties[Recipient]" />
<input type="text" id="address_street" name="properties[Address]" />
...
</div>
<input type="submit" onclick="return validateMyStuff();" value="Submit" />
</form>
Next, your script, slightly more robust:
<script src="/path/to/jquery.js"></script>
<script type="text/javascript">
// this will run when the document's done loading...
$(function(){
$("#ship_to_billing").change(function(){
// make sure of the checked state...
// hide the fields if the checkbox is checked...
// 'this' is the checkbox...
if ($(this).is(":checked")) // fixed error here! ;)
$("#shipping_info").hide();
else
$("#shipping_info").show();
});
});
// validate the input, only if the checkbox is not checked...
// this will be called when your submit button is clicked...
function validateMyStuff() {
if (!$("#ship_to_billing").is(":checked")) {
// check to see that the other input fields have values...
var inputs = $("#shipping_info input");
var ready = true;
inputs.each(function(){
// 'this' is the current input we're validating...
if ($(this).val() == '') {
ready = false;
return false; // exits jQuery '.each' function...
}
});
if (!ready) {
alert("You forgot to fill in something!");
return false;
}
}
return true;
}
</script>
Well, that's the best interpretation I can give based on the limited information in the original question. Hope this helps! :)
EDIT 1:
My apologies! I left out a closing bracket )
on line 10 of my JavaScript code! I've fixed the problem, and copy / pasting the code above into an HTML page seems to work fine. Remember to include your jQuery script above the rest of your scripts, and to put your scripts in the <head>...</head>
section of your HTML.