jQuery.validator.addMethod("maxWords", function(value, element, params) { 
    return this.optional(element) || value.match(/\b\w+\b/g).length < params; 
}, "Please enter {0} words or less."); 
 
// Checkdate - Validate a Gregorian date, from an ISO formatted date string (YYYY-MM-DD) - as per PHP function checkdate()
// @param can pass in either:
		// bool TRUE to validate a single field
		// OR
		// a string of 3 DOM ID's in the sequence Year, Month, Day e.g. #form_user_dob_year #form_user_dob_month #form_user_dob_day
		// Note: only assign the validation rule to 1 of the 3 fields. e.g "user[dob][Year]":	{ required:true, checkdate:'#form_user_dob_year #form_user_dob_month #form_user_dob_day' },
jQuery.validator.addMethod("checkdate", function(value, element, param) {
	
	// if validating 3 fields, replace the validating 'value' with the 3 field values, in ISO format YYYY-MM-DD
	if(param && param !== true)
	{
		var paramArray = param.split(' ');
		var pYear = $(paramArray[0]).val();
		var pMonth = $(paramArray[1]).val();
		var pDay = $(paramArray[2]).val();
		value = pYear + '-' + pMonth + '-' + pDay;
	}
	
	// validate against jquery validator's dateISO: check
	// http://docs.jquery.com/Plugins/Validation/Methods/dateISO
	var validDate =	/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(value);
	
	if(validDate)
	{
		var dateArray = value.split('-');
		var year = dateArray[0];
		var month = dateArray[1];
		var day = dateArray[2];

		// PHP.js checkdate validation
		// http://kevin.vanzonneveld.net
		// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +   improved by: Pyerre
		// *     example 1: checkdate(12, 31, 2000);
		// *     returns 1: true
		// *     example 2: checkdate(2, 29, 2001);
		// *     returns 2: false
		// *     example 3: checkdate(03, 31, 2008);
		// *     returns 3: true
		// *     example 4: checkdate(1, 390, 2000);
		// *     returns 4: false
		var myDate = new Date();
		myDate.setFullYear( year, (month - 1), day );
		var success = month >= 1 && month <= 12 && year >= 1 && year <= 32767 && ((myDate.getMonth()+1) == month && day<32);

		return this.optional(element) || success;
	}
}, jQuery.validator.messages.checkdate);

jQuery.validator.addMethod("barcode", function(value, element) {
	return this.optional(element) || /^\d{13}$/.test(value);
}, "Please enter a 13 digit barcode"); 