By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,162 Members | 1,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,162 IT Pros & Developers. It's quick & easy.

How to handle an apostrophe in a text field

P: 1
Hello,

I am brand new to .ASP and am being asked to debug a problem with handling apostrophes. I'm aware of the Replace function, but I'm not sure where in the code I should insert it.

I've attached a zip file with the 2 relevant .asp pages in it, and to replicate the problem, take the following steps:

Go to http://www.larter.com/v2/collections.asp?c=sig&i=1&s=34100219BW--00

Click on "Inquire"
You will be brought to the /locator.asp page and asked to fill in information. Only the Zip, Distance, and Email are required.

Use the Zip Code 08530 and select 15 miles for the distance.

The first entry should be Saunder's in Flemington, NJ.

The locator.asp page populates store information into the sidebar next to the map, and the Send Inquiry button calls the sendinquiryemail.asp page. The problem is that a store name with an apostrophe will give the following error when you click the button:


CDO.Message.1 error '8004020d'

At least one of the From or Sender fields is required, and neither was found.

/v2/sendinquiryemail.asp, line 121



When I use the Inspect Element function in Chrome, it shows the button's code with the data already populated, and the S after the apostrophe is broken off from the name. I am aware of the Replace("Field", "'", "''") code, but as I am very new to .asp I can't tell exactly where the store name is called from the database and where I should insert this code. Can anyone help me with this? Thanks in advance for any advice!

Doug Coyne
Attached Files
File Type: zip Bytes.zip (15.1 KB, 36 views)
Sep 23 '14 #1
Share this Question
Share on Google+
1 Reply


P: 3
Hi dccoyne67,

I just had to figure this out myself, and with Company names, so I am happy to share how I managed it.
I did get a little lost in your code so I decided to just tell you how I do it and you can apply it to your specific situation.

Disclaimer: as I have a BBA in Information Management Systems from WAY long ago, but am self-taught for ASP, this may not be the BEST way, but it worked for me. If anyone else has any better thoughts, please improve on my suggestions. Also, I'm thinking that some of the Replace functions in my code may be overkill, but after much trial and error, this is how I finally removed all the errors and surprises, so I decided to run with it. One of these days, I'll go back and see if I can "clean" it up a bit. But I'm in no rush.

In my application, about half of my pages Post back to themselves, and I access the data via request.form, and half are loaded via a hyperlink, so I access the data from request.querystring. I discovered I needed to handle these two a little differently when loading the page and the variables for access and use. I'll address both for you. Also, I use the HTML code for apostrophe in my replace.

First, to address hyperlinking and request.querystrings

Before you build your querystring to send out, any field that has a potential apostrophe in it (street names can, too, etc.), should have the Replace function run on it. You can either

1. Perform this task as the querystring is being constructed, or
2. Assign the company name to a variable first, run Replace as part of the assignment, and then use the variable in the querystring.

I do both depending on my need.

Since I pass Company IDs and Company Names a lot, I set up a variable called linkQryStrg at the beginning of those page to maximize integrity. To set that variable, I use this code:

Expand|Select|Wrap|Line Numbers
  1. linkQryStrg="&qCmpID="& vCmpID&"&qCompany="&replace(vCompany,"'","'")
where the variable names with "q" are part of the querystring, and the variable names with the "v" are ASP variables being passed in.

Then, to create the hyperlink, it just looks like:

Expand|Select|Wrap|Line Numbers
  1. ...<a href='frmToGoTo.asp?qAction=Add&qID="&vID&linkQryStrg tabindex=29 onclick='return chkDirty(this)'>ADD NEW ITEM</a>...
Occasionally, I only need to send the company name and in only one place on a particular page, so I don't bother with the variable assignment. I just say:

Expand|Select|Wrap|Line Numbers
  1. ...<a href='frmToGoTo.asp?qAction=Add&qID="&vID&replace(vCompany,"'","'")&" tabindex=29 onclick='return chkDirty(this)'>ADD NEW ITEM</a>...
That covers creating the querystring for sending. When it gets to the destination page, use the Replace function as well when assigning the querystring value to its variable. One of my first variable assignments is always vCompany. The code is:

Expand|Select|Wrap|Line Numbers
  1. vCompany=replace(request.querystring("qCompany"),"'","'")
Additionally - anywhere on the page I refer to, display, or store the variable, I also use the Replace function. (This is because I also load these particular pages as a Post and you will see later that the Replace function is not run when assigning the variable then.)

For example:
1. When stored in a Hidden text field (so it can be accessed during a Post):

Expand|Select|Wrap|Line Numbers
  1. response.write("<td><input type=hidden name=txtCompName value='"&replace(vCompany,"'","'")&"'></td>"&chr(13))
OR
2. When just being displayed on the page:
Expand|Select|Wrap|Line Numbers
  1. response.write("<td colspan=3 class=dealer>"&replace(vCompany,"'","'")&"</td>"&chr(13))
The only time I do not run the Replace function on the Company value is when I am assigning variables from request.form values. In that case, the code looks like:
Expand|Select|Wrap|Line Numbers
  1. vCompany=request.form("txtCompName")
and the linkQryStrg is just:
Expand|Select|Wrap|Line Numbers
  1. linkQryStrg="&qCmpID="&vCmpID&"&qCompany="&vCompany
But everywhere else on the page, vCompany still has the Replace function run on it as shown above.

Again, adapt this to your needs. If it is a little confusing, please let me know your questions and I will clarify. Good Luck!
Oct 7 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.