image validation in javascript

function isValidImage(imagename)
{

imagefile_value = imagename;
var checkimg = imagefile_value.toLowerCase();
if (!checkimg.match(/(\.jpg|\.gif|\.png|\.JPG|\.GIF|\.PNG|\.jpeg|\.JPEG)$/))
{
return false;
}
else
{
return true;
}
}

To calculate date difference between two given dates

function timeDifference(startDate,endDate)
{

date1 = startDate;
date2 = endDate;

laterdate = date1.split(“-“);
laterY=laterdate[0];
laterM=laterdate[1];
laterD=laterdate[2];

earlierdate = date2.split(“-“);
earlierY=earlierdate[0];
earlierM=earlierdate[1];
earlierD=earlierdate[2];

var laterdate = new Date(laterY,laterM,laterD);
var earlierdate = new Date(earlierY,earlierM,earlierD);

var difference = laterdate.getTime() – earlierdate.getTime();

var daysDifference = Math.floor(difference/1000/60/60/24);
difference -= daysDifference*1000*60*60*24
var hoursDifference = Math.floor(difference/1000/60/60);
difference -= hoursDifference*1000*60*60
var minutesDifference = Math.floor(difference/1000/60);
difference -= minutesDifference*1000*60
var secondsDifference = Math.floor(difference/1000);

return daysDifference;

}

To check leap year

function IsLeapYear(year)
{
if ((year%4)==0)
{
if ((year%100==0) && (year%400)!=0)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}

URL validation

function checkURL(url)
{
var theurl=url;
var tomatch= /^(http[s]?:\/\/|ftp:\/\/)(www\.)?[a-zA-Z0-9-\.]+\.(com|org|net|mil|edu|ca|co.uk|co.in|com.au|ac.in|gov|gov.in)(\/[A-Za-z0-9\.-])?/;
if (tomatch.test(theurl))
{

return true;
}
else
{

return false;
}
}

validate email address

function echeck(str) {

var at=”@”
var dot=”.”
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
return false
}

if (str.indexOf(at,(lat+1))!=-1){
return false
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
return false
}

if (str.indexOf(dot,(lat+2))==-1){
return false
}

if (str.indexOf(” “)!=-1){
return false
}

return true
}

to check alphanumeric character

function isAlphaNumeric(valueToTest)
{
var myRegxp = /^[\w ]+$/;
if(myRegxp.test(valueToTest) == 1)
{
return true;
}
else
{
return false;
}
}

function to restrict text area character

function checkTextLength(obj,restrictLength,truncFlag)
{
if(obj.value.length>restrictLength){
if(truncFlag) obj.disabled = true;
alert(“Text should not be more than ” + restrictLength + ” characters”);
if(truncFlag) obj.disabled = false;
obj.focus();
if(truncFlag) obj.value = obj.value.substring(0,restrictLength);
return false;
}else{
return true;
}
}

trim function in javascript

function trim(sString) {
sTrimmedString = “”;
if (sString != “”) {
var iStart = 0;
var iEnd = sString.length – 1;
var sWhitespace = ” \t\f\n\r”;

while (sWhitespace.indexOf(sString.charAt(iStart)) != -1) {
iStart++;
if (iStart > iEnd)
break;
}

// If the string not just whitespace
if (iStart <= iEnd) {
while (sWhitespace.indexOf(sString.charAt(iEnd)) != -1)
iEnd–;
sTrimmedString = sString.substring(iStart,++iEnd);
}
}
return sTrimmedString;
}

validate credit card number using javascript

function isValidCreditCard(cardNo)
{
var str=cardNo;
var digitsOnly = str.replace(/ /g,”);//replace unwanted space
var sum = 0;
var digit = 0;
var addend = 0;
var timesTwo = false;
var i;
for(i = digitsOnly.length-1;i >= 0; i–)
{
digit = parseInt(digitsOnly.substring(i,i+1));
if (timesTwo)
{
addend = digit * 2;
if (addend > 9)
{
addend -= 9;
}
}
else
{
addend = digit;
}
sum += addend;
timesTwo = !timesTwo;
}

var modulus = sum % 10;
return modulus == 0;

}