type:
snippet
category:
checkout
name:
Age Validation For Alcohol Purchase
versions:
1.0, 1.1
tags:
date
date:
2013-04-26

Age Validation For Alcohol Purchase

This snippet will force the user to enter their birthday and confirms that they are 21 years old prior to checkout.

Code

Put this code in your checkout template. Version 1.0 required.

^^custom_begin^^
 
<h2>Additional Information</h2>
 
<fieldset id="fc_custom_fields">
	<div class="fc_inner">
		<div>
			<h4>Date of Birth</h4>
			<select name="h:Month" id="birthmonth">
				<option value="">-- Month --</option>
				{% for i in 1..12 %}
					{% set temp_date = "2010-" ~ i ~ "-01" %}
					<option value="{{ i }}">{{ temp_date|date("F") }}</option>
				{% endfor %}
			</select>
 
			<select name="h:Day" id="birthday">
				<option value="">-- Day --</option>
				{% for i in 1..31 %}
					<option value="{{ i }}">{{ i }}</option>
				{% endfor %}
			</select>
 
			<select name="h:Year" id="birthyear">
				<option value="">-- Year --</option>
				{% for i in 1910..2013 %}
					<option value="{{ i }}">{{ i }}</option>
				{% endfor %}
			</select>
 
			<label class="fc_error" style="display:none;" id="is21error">You must be 21 years or older to order wine from our site.</label>
		</div>
	</div><!-- .fc_inner -->
</fieldset><!-- #fc_custom_fields -->

 
<script type="text/javascript">
function ageCheckDate() {
	if (jQuery("#birthmonth").val() == "" || jQuery("#birthday").val() == "" || jQuery("#birthyear").val() == "") return false;
	var birthday_string = jQuery("#birthmonth").val() + "/" + jQuery("#birthday").val() + "/" + jQuery("#birthyear").val();
	var birthday = new Date(birthday_string);
	var today = new Date();
	var years = today.getFullYear() - birthday.getFullYear();
	birthday.setFullYear(today.getFullYear());
	if (today < birthday) years--;
	if (years < 21) return false;
	return true;
}
function ageCheck() {
	is_21 = ageCheckDate();
	if (is_21) {
		FC.checkout.config.isValid = true;
		jQuery("#is21error").hide();
	} else {
		FC.checkout.config.isValid = false;
		jQuery("#is21error").show();
	}
}
FC.checkout.overload("validateAndSubmit", "ageCheck", null);
</script> 
^^custom_end^^

Site Tools