Empowering Smart Business (sm)

UDFs: isTime()

The future of your business is an open road, JPG image (20.8kb)

containsLeapYear()

Function that validates if a string is a valid time in 12- or 24-hour format.

<CFSCRIPT>
/**
 * Validates if a string is a valid time in 12- or 24-hour format
 * 
 * @param time		The string to validate.
 * @param format	The required format.  Valid options are "12" and "24".  Leave blank for both.
 * @return Returns a boolean.
 * @author Mosh Teitelbaum
 * @version 1, February 1, 2010
 */
function isTime(time) {
	if ( (ArrayLen(Arguments) eq 2) AND (Arguments[2] is "12")) {
		return ReFindNoCase("^((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [ap]m))$", time);
	} else if ( (ArrayLen(Arguments) eq 2) AND (Arguments[2] is "24")) {
		return ReFindNoCase("^([01]\d|2[0-3])(:[0-5]\d){0,2}$", time);
	} else {
		return ReFindNoCase("^([01]\d|2[0-3])(:[0-5]\d){0,2}$|^((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [ap]m))$", time);
	}
}
</CFSCRIPT>

This UDF is also available from cflib.org at http://www.cflib.org/udf/isTime.