• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Lesson 9. other functions > Using Trim to Remove Extra White Space

Using Trim to Remove Extra White Space

When you are inserting values into a database, you want to make sure that no excess white space finds its way into the value to be entered. This could happen when a user fills out a form and accidentally has a forward or trailing space in their entry. You don't want these extra forward or trailing spaces in the database. ColdFusion provides a function that removes any extra spaces at the beginning or end of a string; however, it doesn't remove white space that lies in between values (for example, a space between a two words in a sentence).

Open X:\Lesson9\Start\productinsert.cfm.

This is a copy of the same template that you used to insert products into the database.

Wrap the Trim function around each one of your form variables using the following syntax: '#trim(variablename)#'.

This will not affect the variable in any way other than removing the extra white space.


ColdFusion also offers the more specified functions Rtrim and Ltrim. As you can imagine, these functions perform the same task as Trim, except that they remove white space from only the right or left side depending on which of the functions you use. Give it a try!

Save the file to the Lesson6\Start\ folder in the Inetpub\wwwroot folder.

You will be overwriting the previous productinsert template.

Open a Web browser and type into the address window.

This is the same form you used before to insert products; it hasn't changed.

Fill out the form, making sure to add spaces before and after your entries, and click Add Product.

Once you click Add Product, the product will be added and you will be returned to the Add Product form. What happened to the extra spaces you added? They should be gone.

Open Microsoft Access and open the products database from Inetpub\databases.

You are going to go into the actual database to see that the white space was indeed removed.

Open the tblProducts table and look at your last entry with the spaces.

You will notice that the extra spaces you entered before the values are gone. It would be hard to tell if the spaces were present after the value, but the trim function definitely removed them.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint