PDA

Click to See Complete Forum and Search --> : Javascript-Trim functions


ranjith
Mar 15th, 2001, 10:25 AM
Can any one tell me,what are the equivalent functions in Javascript for trim,rtrim ,and ltrim.

Mark Sreeves
Mar 16th, 2001, 01:01 AM
There's no JavaScript trim() function as such. This should suit your needs though....




<script language="JavaScript">
<!--
function trim(strText) {
// this will get rid of leading spaces
while (strText.substring(0,1) == ' ')
strText = strText.substring(1, strText.length);

// this will get rid of trailing spaces
while (strText.substring(strText.length-1,strText.length) == ' ')
strText = strText.substring(0, strText.length-1);
return strText;
}
//--></script>




The script came from here: http://developer.irt.org/script/1310.htm

dbelley_office
Dec 8th, 2005, 02:56 PM
Hi, using the above trim function why is this not working ?


<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">

function ValidateForm(form)
{
//This line works
alert ('*' + form.txtCourriel.value);

// This line does not work
alert ('*' + trim(form.txtCourriel.value));
// This line does not work
alert ('*' + trim(form.txtCourriel));
}

</SCRIPT>


How do I use that trim function ?

Guru
Dec 9th, 2005, 03:08 AM
Jeeze that's an old thread!


alert ('*' + trim(form.txtCourriel.value));



This is correct and it works fine for me.