Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Single & Double Quotes from DB to Input Field

Status
Not open for further replies.

CamaroLT

Programmer
Dec 16, 2002
159
CA
I realize there is a lot of ASP code here, but, the resolution I'd like to come up with (Although a direct ASP solution would be great) seems to lead me to JavaScript.

I am having an issue attempting to get a single and double-quote value into a forms text input field. The web page is being drawn up under ASP.

The situation may arise that when pulling information from a DBMS, a particular field may have a single quote, double quote or both single and double quotes.

For example, a field may have

[highlight]Aren't you "GOING" to do that work now?[/highlight]​

I have no problem returning the data and putting it to the screen so long this text is not going into an input text box. It comes out perfectly fine in straight HTML text.

I've tried using replace to change the single quote into two single quotes, right in the input box. (IE: value='Aren''t you "GOING" to do that work now?') I've tried switching things around so that I use value=" instead of value=', I've tried going to javascript and use something like replace(Val, "'", "+chr(39)+") within the ASP. Still no go. The field just comes up blank. Looking at the source, everything is alright.

So far, in the final source output, I've had the following:

[highlight]<script>document.CompanyForm.CompanyName.value='Company ''1 with a single quote';</script>[/highlight]

[highlight]<script>document.CompanyForm.CompanyName.value='Company '+chr(39)+'1 with a single quote';</script>[/highlight]

I've tried a couple other renditions from other sites pertaining to a function in the string unit, to no avail.

What little thing am I missing here??

The ASP attempt of code I've used is the following:
Code:
function WriteInput(Val,Name)
	writeInput=vbcrlf&"<input type=text name="&Name&" maxlength=255 size=100>"
	writeinput=writeinput&"<script language='JavaScript'>document.CompanyForm."&Name&".value='"
	WriteInput=WriteInput&replace(val,"'","'+chr(39)+'")
	WriteInput=WriteInput&"';</script>"
	'WriteInput=vbcrlf&Val
end function

I really do need something that will check and apply the proper text to the input box for both single and double quotes.

Just to note, if there is no quote in the field, the input box is filled properly.

-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=
NEVER send the boss to do a techs job
 
I've tried using replace to change the single quote into two single quote
Try replacing the " with the correct HTML entity: &quot;
Code:
Aren't you "GOING" to do that work now?
would become
Code:
Aren't you &[b][/b]quot;GOING&[b][/b]quot; to do that work now?
There's probably some predefined method in ASP that will do this automagically to a string. I know there is in PHP.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
I've tried that, and what you've written in the CODE section comes out appearing in the input box. So when I submit the form, the &quot; goes into the database, which is not correct. True that this information will appear great on an HTML form, but the other portion of this entire project relies on the fact that the database information is kept exactly as its supposed to appear. Single quotes and double quotes. The issue I'm having is getting the input box to show a single quote or double-quote. :(

-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=
NEVER send the boss to do a techs job
 
Another attempt I just tried, which just gives me the first segment of text:

Code:
<input type=text name=Name maxlength=255 size=100 value='Ayr Farmers'+chr(39)+' Mutual Ins. Co.'>

*pounds head on desk*

-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=
NEVER send the boss to do a techs job
 
I think you will have to convert all " into &quot; when delivering the page... and convert all &quot; into " before putting the data into the database. That way you get to view the data in it's intended context -- you are storing it no differently.

When I did this, it worked as I expected (Safari):
Code:
<input type="text" value="Hello &quot;World&quot;!"/>

Cheers,
Jeff

PS: Mind that head! [smile]

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
I must have done something incorrect when playing with the &quot; because I KNOW I did that, and it actually put the literal &quot; in the input box.

The data is being stored AS IS in the database, incase another program needs to link into it. This way if some sort of mutation to the data has to occur, the language is responsible for the translation, and plays by its own rules, instead of relying on the rules set by other applications. I realize that all applications need to play by the same rules, but having C or Delphi or VB interpret &quot; for example is more work for those three languages.

Thanks for the input. ... err.. no pun intended. ;)

-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=
NEVER send the boss to do a techs job
 
It seems that replacing " with &quot; doesn't work when we use DOM to set the value of the input element.

Code:
document.forms["myForm"].elements["myTextField"].value = "Hello &quot;World&quot;!";

The &quot; comes out as &quot; Any idea how I can fix this if I want to use DOM to set the value?

Regards,
Min
 
>[tt]<input type=text name=Name maxlength=255 size=100 value='Ayr Farmers'+chr(39)+' Mutual Ins. Co.'>[/tt]
Double quotes as desired:
[tt]<input type=text name=Name maxlength=255 size=100 value='Ayr Farmers'+'\u0022'+' Mutual Ins. Co.'>[/tt]
Single quotes if desired:
[tt]<input type=text name=Name maxlength=255 size=100 value='Ayr Farmers'+'\u0029'+' Mutual Ins. Co.'>[/tt]
Direct embed in the string works the same. Hence, the replacement can be done in this approach.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top