Skip to content

Instantly share code, notes, and snippets.

@dervalp
Created January 9, 2012 19:55
Show Gist options
  • Save dervalp/1584618 to your computer and use it in GitHub Desktop.
Save dervalp/1584618 to your computer and use it in GitHub Desktop.
Control for Birthdate with Date.js
//reference Date.js
if (!Utilities) var Utilities = {};
$.extend(Utilities,
{
Birthdate : function(lg) {
var Month = {
en : ["January","February","March","April","May","June","July","August","September","October","November","December"],
fr : ["janvier","février","mars","avril","mai","juin","juillet","août","septembre","octobre","novembre","décembre"]
}
function init()
{
this.lg = lg;
$(".birthdate").each(function(){
_configureBirthDate($(this));
});
}
init();
function _getMonthHtml(month)
{
var beginselect = '<select class="dobfield month">';
var monthList = Month[this.lg];
var content = "";
for(var i = 0; i < (monthList.length-1); i++)
{
if(month === i) {
content += "<option selected=selected value='" + i + "'>" + monthList[i] + "</option>";
} else {
content += "<option value='" + i + "'>" + monthList[i] + "</option>";
}
}
var endSelect = '</select>';
return (beginselect + content + endSelect);
}
function _getDayHtml(day)
{
if(day) {
if(day < 10)
{
day = "0"+day;
}
return '<input type="text" class="day dobfield" value="' + day + '"/>';
} else {
return '<input type="text" class="day dobfield" placeholder="DD" value=""/>';
}
}
function _getYear(year)
{
if(year) {
return '<input type="text" class="yearlong dobfield" value="' + year + '"/>';
}
else {
return '<input type="text" class="yearlong dobfield" placeholder="YYYY" value=""/>';
}
}
function _configureBirthDate(el)
{
var value, inititalValue, year, month, day, content;
value = el.val();
inititalValue = Date.parse(value);
if(inititalValue)
{
year = inititalValue.getFullYear();
month = inititalValue.getMonth();
day = inititalValue.getDate();
}
content = _getMonthHtml(month) + _getDayHtml(day) + _getYear(year);
el.parent().append(content);
//bind event
$('.dobfield').change(function () {
var triggerChange = true;
var parent = $(this).parent();
var answer = parent.find('.birthdate');
var yearval = parent.find('.yearlong').val();
var dayval = parent.find('.day').val();
var monthval = parent.find('.month').val();
//Test Year
yearInInt = parseInt(yearval);
if (!isNaN(yearInInt) && Date.validateYear(yearInInt)) {
if (yearval > Date.today().getFullYear()) {
$(parent.find('.yearlong')).addClass('error');
triggerChange = false;
} else {
$(parent.find('.yearlong')).removeClass('error');
}
}
else {
triggerChange = false;
$(parent.find('.yearlong')).addClass('error');
}
//Fix the day when you put a 0 in front of a single number
if (dayval.length > 0 && dayval.charAt(0) == '0') {
dayval = dayval.substr(1, dayval.length);
}
dayinInt = parseInt(dayval);
if (!isNaN(dayinInt) && dayinInt <= 31 && dayinInt > 0) {
try {
Date.validateDay(dayinInt, yearInInt, (parseInt(monthval) - 1));
$(parent.find('.day')).removeClass('error');
}
catch (err) {
$(parent.find('.day')).addClass('error');
triggerChange = false;
}
} else {
$(parent.find('.day')).addClass('error');
}
if (triggerChange) {
var date = Date.today().set({ year: parseInt(yearval), month: parseInt(monthval) - 1, day: parseInt(dayval) });
answer.val(date.toISOString()).change();
} else {
answer.val(date);
}
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment