Using Trim to Remove Extra White Space

I l @ ve RuBoard

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 ).

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

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

  2. 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.

    NOTE

    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!

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

    You will be overwriting the previous productinsert template.

  4. Open a Web browser and type http://127.0.0.1/Lesson6/Start/productinsertform.cfm into the address window.

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

  5. 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.

  6. 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.

  7. 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.

I l @ ve RuBoard


Macromedia ColdFusion 5. Training from the Source
Macromedia ColdFusion 5 Training from the Source (With CD-ROM)
ISBN: 0201758474
EAN: 2147483647
Year: 2002
Pages: 99
Authors: Kevin Schmidt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net