Created
June 12, 2012 16:34
-
-
Save Demwunz/2918572 to your computer and use it in GitHub Desktop.
Ender Age Gate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SITE = { | |
common: { | |
init: function(){ | |
SITE.functions.addOptions(); | |
} | |
}, | |
functions : { | |
ageGate : function(){ | |
var min_age = 18; | |
// | |
$('#age-form').submit(function(event){ | |
event.preventDefault(); | |
$('.error').addClass('initial'); | |
// grab the values | |
var day = parseInt($('#age-days').val()); | |
var month = parseInt($('#age-months').val()); | |
var year = parseInt($('#age-years').val()); | |
// get the user dob and todays date | |
var dob = new Date((year + min_age), month, day); | |
var today = new Date(); | |
// if ok, let them through otherwise warn them | |
if ((today.getTime() - dob.getTime()) < 0) { | |
console.log('you are too young'); | |
} else { | |
console.log('come on in!'); | |
} | |
}); | |
}, | |
addOptions : function(){ | |
var options = { days : 31, months : 12, years : 100 }; | |
// | |
$.each(options, function(key, value){ | |
SITE.functions.buildDropdowns(key, value); | |
}); | |
}, | |
buildDropdowns : function(key, value){ | |
var date = new Date(); | |
var this_year = date.getFullYear(); | |
var wrapper = $(document.getElementById(key)); | |
var options = []; | |
var max = value; | |
// reverse while loop | |
while(max--){ | |
// if years then minus from the current year, otherwise carry on as normal | |
var val = Math.abs(key === 'years' ? this_year - max : max + 1); | |
// store this option | |
options.push('<option value="'+ val +'">'+ val +'</option>'); | |
// if we're at the end then join everything together and attach to page | |
if(max === 0){ | |
wrapper.append('<select id="age-'+ key +'">' + options.reverse().join('') + '</select>'); | |
if(key === 'years'){ | |
SITE.functions.ageGate(); | |
} | |
} | |
} | |
} | |
} | |
} | |
$(document).ready(function(){ | |
SITE.common.init(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment