Removing Beginning and Ending Spaces

Let’s face it.  As long as people manually enter data, there are going to be typos in your data.  One common error is when a user adds extra spaces at the beginning or end of a form field.  So how do you stop this?

First, create your /Form.asp page and copy the code below:

<form name="YourFormName" method="Post" action="confirm.asp">
Email:  <input type="text" name="Email" size="50"><br>
First Name:  <input type="text" name="FirstName" size="50"><br>
Last Name:  <input type="text" name="LastName" size="50"><br>
Subject:  <input type="text" name="Subject" size="50"><br>
Comments:  <textarea cols="50" name="Comments" ></textarea><br>
<input type="submit" name="Submit" value="Submit">

Then, you use the TRIM function in your /Confirm.asp page to remove any spaces:

DIM strEmail, strFirstName, strLastName, strSubject, strComments
strEmail = TRIM(Request.Form("Email"))
strFirstName = TRIM(Request.Form("FirstName"))
strLastName = TRIM(Request.Form("LastName"))
strSubject = TRIM(Request.Form("Subject"))
strComments = TRIM(Request.Form("Comments"))

The TRIM() function will remove any extra spaces from the beginning and end of each form field here, which helps to improve consistency within your database data.  If you are only concerned with removing spaces from the left or right side of the text, you could alternatively use LTRIM() or RTRIM() respectively.  That’s it, TRIM away!

< Back to ASP Scripts