Monday, October 29, 2007

JavaScript: Some Useful Functions

toUpperFirstChar:
Accepts an object of type "Text" and converts the first character of the string to upper case.
Parameters: Object
Returns: n/a

Here is the defination:

function toUpperFirstChar(obj)
{
LTrimObj(obj);
obj.value = obj.value.substr(0,1).toUpperCase()+obj.value.substr(1,obj.value.length-1);
}


checkDateValidity:-
This method checks if the fDt is less than tDt or not.
Parameters: FromDate, ToDate
Returns: Boolean

Here is the defination..


function checkDateValidity(fDT,tDT)
{
var fyear=fDT.charAt(7)+fDT.charAt(8)+fDT.charAt(9)+fDT.charAt(10);
var tyear=tDT.charAt(7)+tDT.charAt(8)+tDT.charAt(9)+tDT.charAt(10);

var fm=fDT.charAt(3)+fDT.charAt(4)+fDT.charAt(5);
var tm=tDT.charAt(3)+tDT.charAt(4)+tDT.charAt(5);
var fmonth=MonthReplace(fm);
var tmonth=MonthReplace(tm);

var fday=fDT.charAt(0)+fDT.charAt(1);
var tday=tDT.charAt(0)+tDT.charAt(1);

fDate = new Date(fyear,fmonth-1,fday);
tDate = new Date(tyear,tmonth-1,tday);

if(fDate > tDate)
{
alert("From date must not be greater than to date");
return false;
}
else
{
return true;
}
}


varifyPassword:-
varifies if the password is more than 6 char long and it contains atleast one digit and 4 distinct characters.

Here is the defination of the function:

function varifyPassword(pass)
{
pass=Trim(pass);
var validFlag = 1;
if(pass.length < 6)
{
validFlag = 0;
}
else
{
alphaList=new Array();
numList=new Array();
var flag=0;
for(var i=0; i< pass.length; i++)
{
var oneChar=pass.charAt(i)
if(isAlphabet(oneChar))
{
var charFlag=1;
for(var p=0; p {
if(oneChar==alphaList[p])
{
charFlag=0;
break;
}
}
if(charFlag)
{
alphaList[alphaList.length]=oneChar;
}
}
else if(oneChar>=0 && oneChar <=9)
{
var numFlag=1;
for(var p=0; p {
if(oneChar==numList[p])
{
numFlag=0;
break;
}
}
if(numFlag)
{
numList[numList.length]=oneChar;
}
}
else
{
validFlag = 0;
break;
}


if ((alphaList.length >= 1) && (numList.length>= 1) && ((alphaList.length + numList.length) >= 4))
{
flag = 1;
break;
}
}

if(flag)
{
return true;
}
else
{
validFlag = 0;
}
}

if (! validFlag)
{
alert("The password is not valid, or needs to be updated. Consult the help page for instructions about choosing a valid password.")
return false;
}
}

1 comment:

Moni said...

For which date format is this date validation function used:-- can any one tell me .... ??