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;
}
}

JSP: sendRedirect() vs. getRequestDispatcher()

Today my other team member was facing a problem and the request objects were destroed when he was using request.sendRedirect(). He was struggling with it and came to me. Actually there is other method to use when you want to pass the origional request objects, that is getRequestDispatcher();


When you want to preserve the current request/response objects and transfer them to another resource WITHIN the context, you must use getRequestDispatcher or getNamedDispatcher.

If you want to dispatch to resources OUTSIDE the context, then you must use sendRedirect. In this case you won't be sending the original request/response objects, but you will be sending a header asking to the browser to issue a request to the new URL.


NOTE: If you don't need to preserve the request/response objects, you can use either.