Tuesday, April 15, 2008

Useful Javascript functions

#function to check a field is empty or not.

function isEmpty(fieldName){
/*
* This method will return true if the field is empty and return
* false if it is not empty.
*/
emptyFlag = true;
if (fieldName.value == ''){ //to check the given value is empty or not
return true;
}
else{
for(i=0;i<(fieldName.value).length;i++)
{
if((fieldName.value).charAt(i)!=''){
emptyFlag = false;
break;
}
}
return emptyFlag;
}
}



#Today's date


var date = new Date();
var d = date.getDate();
var m = date.getMonth() + 1;
var yy = date.getYear();

var day = (d < 10) ? '0' + d : d;
var month = (m < 10) ? '0' + m : m;
var year = (yy < 1000) ? yy + 1900 : yy;

//put the day, month and year in whatever format you require.

var todayDt = year+"-"+month+"-"+day

document.write(todayDt);

No comments: