Wednesday, September 27, 2006

Useful JS functions

//Trim function

function trimAll(sString)
{
while (sString.substring(0,1) == ' ')
{
sString = sString.substring(1, sString.length);
}
while (sString.substring(sString.length-1, sString.length) == ' ')
{
sString = sString.substring(0,sString.length-1);
}
return sString;
}

//Usage
if (trimAll(document.form1.txtForename.value)=="")
{
alert('Please enter ForeName');
document.form1.txtForename.focus();
return false;
}
////////////////////////////////////////////////////////////////

No comments: