Check so at least one checkbox on your form is checked before it’s allowed to submit:

$("#main_form").on('submit',function(e){

   if ( $("input[type='checkbox']:checked").length > 0) {

   }
   else {
         e.preventDefault();

         alert("Please check at least one list you wish to be included in.");
         return false;
   }

   return true;

});

 


In a jQuery form post, here is how you add more data to the serialized form data.  This is useful if you find yourself needing to pass some extra data along with the AJAX post.  This is a barebones example of a blind AJAX post to a form handler script called “form_handler.php”.

I find jQuery’s “serialize” and “serializeArray” functions are very efficient when collecting form data, you can’t get much easier than that.  The beauty of using a framework, whether it’s a Javascript framework, or PHP framework, these built-in libraries and functions save coding time.

var data = $('#myForm').serializeArray();

data.push({name: 'wordlist', value: wordlist});

$.post("form_handler.php", data);

I hope this saves someone time!  This solution works well in many different instances you have to post a form through Javascript.

– Aaron  Belchamber


In case you ever need to manually validate those pesky multi-field credit card date fields, you know “Month” and “Year” and you want to make sure the expiration is never set prior to the current month for obvious reasons like the credit card would get declined, you need to have the browser client side figure out (or be given the value through PHP injection into your JS script) the last date of the last day of the current month so you can then calculate and compare the “Month” and “Year” fields to this date value.

Using Javascript’s Date object should be adequate for most browsers and IE 8 or greater (there’s a work around to accommodate older browsers, but is it really worth reaching people who are using IE7?!  That’s a cost/benefit decision you and your marketing team will have to make.)

dt=new Date();
new Date(dt.getFullYear(), dt.getMonth() + 1, 0, 23, 59, 59)

No Javascript libraries like JQuery is required!