Tip

Trim functionality on web 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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.