Trim functionality on web in JavaScript

Many times we wish that how nice it would be if we would have @Trim functionality in JavaScript.

You Can View User Feedback To This Tip

Many times we wish that how nice it would be if we would have @Trim functionality in JavaScript.

You can have the same.
Use this trim(strln) function.
Just put this function in the JSHeader and call the same like this.

trim(document.forms[0].fieldName.value);

This will trim the whole value, like @Trim function.


//Trim Function
function trim(strIn)
{
	strOut = strIn;

	for (var t = 0; t <= (strIn.length -1); t++ )
	 {		
	 	if (  strIn.charAt(t) != " ")
			{	
				strIn = strOut;			
				for (var I = strIn.length-1 ;I >= 0  ; I-- )
				 {				 		
				    if (  strIn.charAt( I ) != " ")
					{	  
					 	return (strOut) ;	 
					}
				 	else
					{ 
						strOut = strIn.substring(0, I );
					 }   
			   	   }
			 }

		 else
		 { 
			strOut = strIn.substring( t+1, strIn.length);
		}   
	}
	return(strOut);
}

USER FEEDBACK TO THIS TIP

  • I have seen a TIP for @TRIM posted on 07/18/2001 and I tried to use it but it is not removing the redundant spaces in between the characters like " aaa bbb "to "aaa bbb." You can use this JavaScript code in place of the @TRIM formula language function. It will remove all leading, trailing & redundant spaces between a string.

    function trim(inputString)
    { 
    //String1 = inputString.replace(/^s+/,""); // trim leading white space   // this JAVA code is not working sometime with domino.
    //String2 = String1.replace(/s+$/,""); // trim trailing white space     //this JAVA code is not working sometime with domino.
    // added another condition in the end to remove ending space.
     	finalText = "";
          lastChar = " ";
          for(x = 0; x < inputString.length; x++) // with this loop we will delete all redundant spaces
              {
               curChar = inputString.charAt(x);
               if ((curChar != " ") || (lastChar != " "))
                  {
          			finalText += curChar;
                  }
                lastChar = curChar;
               }
          if (finalText.charAt(finalText.length - 1) == " ")  // this will remove the ending/trailing space.
          {
           finalText = finalText.substring(0, finalText.length - 1);
          }
          return finalText;
    }
    — Faran Siddiqy

This was first published in July 2001

Dig deeper on Domino Resources - Part 4

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchWinIT

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchEnterpriseLinux

SearchVirtualDataCentre.co.UK

Close