Microsoft KB Archive/307406

= PRB: ASP Variable in HTML Text Box Is Truncated =

Article ID: 307406

Article Last Modified on 10/11/2001

-

APPLIES TO


 * Microsoft Active Server Pages 4.0

-



This article was previously published under Q307406



SYMPTOMS
If you place an Active Server Pages (ASP) variable in an HTML text box, the variable may be truncated if it contains a space, a double quotation mark, or both.



CAUSE
This problem occurs because the ASP code generates invalid HTML: strSpace = 

strBoth =  By design, if the value is not enclosed in double quotation marks, HTML assumes that the first space that it encounters represents the end of the value and that the following text represents another tag. In the preceding sample code, HTML assumes that the value tags end at &quot;my&quot; and &quot;I'm,&quot; and HTML ignores &quot;space&quot;, &quot;5'-10&quot;&quot;, and &quot;tall&quot; as invalid tags.



RESOLUTION
To resolve this problem, wrap the ASP variable in the Server.HTMLEncode method and in double quotation marks. For more information, see the &quot;More Information&quot; section.



MORE INFORMATION
The following code reproduces this behavior:  ASP Variables: <% strSpace = &quot;my space&quot; strDblQuote = &quot;my&quot;&quot;doublequote&quot; strBoth = &quot;I'm 5'-10&quot;&quot; tall&quot; Response.Write &quot;strSpace = &quot; & strSpace Response.Write &quot; strDblQuote = &quot; & strDblQuote Response.Write &quot; strBoth = &quot; & strBoth %> HTML Text Boxes: strSpace = > strDblQuote = > strBoth = >  The strSpace and strBoth ASP variables are both truncated in the HTML text boxes. However, note that the strDblQuote variable is not truncated.

Resolution 1: Fix the strSpace Variable Only
To fix strSpace, wrap the value in double quotation marks as follows: strSpace = &quot;>

strDblQuote = &quot;>

strBoth = &quot;> Although this solution fixes strSpace, it truncates strDblQuote. The double quotation mark within the strDblQuote variable was originally interpreted correctly as part of the value; now it is interpreted as the end of the value because it is the first double quotation mark that HTML encounters after the initial double quotation mark.

In addition, the strBoth variable is now truncated for a different reason. Originally, the first space was interpreted as the end of the value; now, the double quotation mark is interpreted as the end of the value: strSpace = 

strDblQuote = <INPUT type=&quot;text&quot; value=&quot;my&quot;doublequote&quot;>

strBoth = <INPUT type=&quot;text&quot; value=&quot;I'm 5'-10&quot; tall&quot;>

Resolution 2: Fix All Three Variables
To fix both strDblQuote and strBoth without truncating strSpace, wrap the variables in the Server.HTMLEncode method, as well as in double quotation marks. For example: strSpace = <INPUT type=&quot;text&quot; value=&quot;<%=Server.HTMLEncode(strSpace)%>&quot;>

strDblQuote = <INPUT type=&quot;text&quot; value=&quot;<%=Server.HTMLEncode(strDblQuote)%>&quot;>

strBoth = <INPUT type=&quot;text&quot; value=&quot;<%=Server.HTMLEncode(strBoth)%>&quot;> This fix generates valid HTML for all three variables: strSpace = <INPUT type=&quot;text&quot; value=&quot;my space&quot;>

strDblQuote = <INPUT type=&quot;text&quot; value=&quot;my"doublequote&quot;>

strBoth = <INPUT type=&quot;text&quot; value=&quot;I'm 5'-10" tall&quot;> NOTE: This solution still requires that you wrap the values in double quotation marks as in the first solution; otherwise, strSpace and strBoth are truncated.

<div class="references_section">