Wednesday 13 June 2007

checkDate()

This function checks if the date is in a valid format (yyyy-mm-dd hh:mm:ss) and if the date actually exists.


function checkDate(date)
{
var re = /^\d{4}-\d{2}-\d{2}\s{1}\d{2}:\d{2}:\d{2}$/;

var tDate = date.substr(0,10);
var aDate = tDate.split('-');

var tTime = date.substr(11,19);
var aTime = tTime.split(':');

year = aDate[0];
month = aDate[1] - 1;
day = aDate[2];

hour = aTime[0];
min = aTime[1];
sec = aTime[2];

// we check here the basic date values
if ((year < 1980)) {
return false;
}

if (hour > 24) {
return false;
}

if (min > 60) {
return false;
}

if (sec > 60) {
return false;
}

sourceDate = new Date(year,month,day);
if(year != sourceDate.getFullYear())
{
return false;
}

if(month != sourceDate.getMonth())
{
return false;
}

if(day != sourceDate.getDate())
{
return false;
}

if (!re.test(date)) {
return false;
}
return true;
}

Tuesday 12 June 2007

getMonthLength()

I use this function everytime i am working with some type of time handling or validation.


function getMonthLength(month,year) {
var monthlength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
if (month==1 && (year/4==Math.floor(year/4) ||
year/400==Math.floor(year/400)))
{
return 29;
}
else return monthlength[month];
}

Tuesday 22 May 2007

toggle()

There are probably tons of toggle functions in use and i admit that this one is nothing special but it is very handy for me. The history of ‘toggling’ basically comes down to showing and hiding off elements upon an event being fired. Here is my version of it


function toggle(objId) {
var el = document.getElementById(objId);
if ( el.style.display != 'none' ) {
el.style.display = 'none';
}
else {
el.style.display = '';
}
}

arrayKeyExists()

This is also a function that should be a part of DOM core functionality. It checks if a key exists in an Array()


Array.prototype.arrayKeyExists = function (keyValue) {
for (key in this) {
if (key == keyValue) {
return true;
}
}
return false;
};

inArray()

This is just a simple function that extends the DOM Array object. It checks if a value is in Array()



Array.prototype.inArray = function (value) {
var c;
for (c=0;c < this.length; c++) {
if (this[c] === value) {
return true;
}
}
return false;
};