<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Accidental Developer &#187; textarea</title>
	<atom:link href="http://osric.com/chris/accidental-developer/tag/textarea/feed/" rel="self" type="application/rss+xml" />
	<link>http://osric.com/chris/accidental-developer</link>
	<description>What if Gregor Samsa awoke a computer programmer?</description>
	<lastBuildDate>Tue, 25 May 2010 20:21:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Javascript textarea counter</title>
		<link>http://osric.com/chris/accidental-developer/2008/11/javascript-textarea-counter/</link>
		<comments>http://osric.com/chris/accidental-developer/2008/11/javascript-textarea-counter/#comments</comments>
		<pubDate>Sat, 15 Nov 2008 19:33:26 +0000</pubDate>
		<dc:creator>Chris Herdt</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[delicious]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[textarea]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://osric.com/chris/accidental-developer/?p=109</guid>
		<description><![CDATA[I&#8217;ve been thinking more about the textarea counter issue that I mentioned in my previous post (&#8220;Users Paste Differently&#8220;).
First of all, I noticed that some of the textarea counter scripts date back to at least 2000, so this has been a problem that developers have been looking to solve for 8 years. I checked the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been thinking more about the textarea counter issue that I mentioned in my previous post (&#8220;<a href="http://osric.com/chris/accidental-developer/?p=103">Users Paste Differently</a>&#8220;).</p>
<p>First of all, I noticed that some of the textarea counter scripts date back to at least 2000, so this has been a problem that developers have been looking to solve for 8 years. I checked the <a href="http://www.w3.org/html/wg/html5/">HTML 5 specification</a> and found that in HTML 5, the <a href="http://www.w3.org/html/wg/html5/#the-textarea-element">textarea element has a maxlength attribute</a>. Presumably user agents will build in the most elegant solution.</p>
<p>But what is the current most elegant solution?<span id="more-109"></span><br />
I checked a couple sites that I know use textarea counters well: <a href="http://delicious.com/">Delicious</a> and <a href="http://twitter.com/">Twitter</a>.</p>
<p><strong>Delicious</strong><br />
<div id="attachment_118" class="wp-caption alignnone" style="width: 420px"><a href="http://osric.com/chris/accidental-developer/wp-content/uploads/2008/11/delicious-textarea-counters.png"><img src="http://osric.com/chris/accidental-developer/wp-content/uploads/2008/11/delicious-textarea-counters.png" alt="Textarea counters on Delicious.com" title="delicious-textarea-counters" width="410" height="66" class="size-full wp-image-118" /></a><p class="wp-caption-text">Textarea counters on Delicious.com</p></div></p>
<p><strong>Twitter</strong><br />
<div id="attachment_120" class="wp-caption alignnone" style="width: 394px"><a href="http://osric.com/chris/accidental-developer/wp-content/uploads/2008/11/twitter-textarea-counters.png"><img src="http://osric.com/chris/accidental-developer/wp-content/uploads/2008/11/twitter-textarea-counters.png" alt="Textarea counters on Twitter.com" title="twitter-textarea-counters" width="384" height="80" class="size-full wp-image-120" /></a><p class="wp-caption-text">Textarea counters on Twitter.com</p></div></p>
<p>I like how prominent the character count is in Twitter, although perhaps too terse, but if Javascript is disabled it displays nothing at all. On the other hand, in Delicious displays &#8220;1000 characters max&#8221; if Javascript is off (instead of &#8220;1000 characters left&#8221;), which is still useful information. (I would avoid using the abbreviation <em>max</em>, though, and use <em>maximum</em> or <em>limit</em> instead, which might be better understood by those for whom English is a second language.)</p>
<p>I like Twitter&#8217;s &#8220;warning track&#8221; that lets the user know they need to keep it concise and wrap it up, although I find it confusing that they used bright red to denote both the &#8220;dangerously close to&#8221; as well as &#8220;over&#8221; the limit. The positive bright red numbers could easily be misconstrued as over the limit, since red is a color we frequently use to denote errors.</p>
<p>Twitter&#8217;s counter responds to onBlur and onChange, whereas the counter in Delicious is triggered by neither.</p>
<p>Both get one thing right that I think most other textarea counters get wrong: they don&#8217;t truncate the user&#8217;s text. Deleting something that the user has typed (or pasted) is definitely a bad idea for at least two reasons: the user may not realize the input was truncated and may submit incomplete info, and that the user, upon discovering that the input requires editing, may choose to cut text from someplace other than the end. Instead, both versions alert the user that they are over the limit and provide information on how many characters need to be cut to stay within the limit.</p>
<p>(As an aside, one thing I find curious is that both count an <em>Enter</em> keystroke as one character. I&#8217;ve run into issues with this before because a line break on a Windows-based system should insert 2 ASCII characters: a Carriage Return (CR) and a Line Feed (LF). *nix systems, including OS X, will insert a Line Feed (LF). However, I haven&#8217;t been able to reproduce this issue with recent testing. If it is still an issue, I imagine they handle this on the back-end by converting the CRLF to LF before inserting it into the database?)</p>
<p>In short, my recommendations are:</p>
<ul>
<li>Don&#8217;t truncate the user&#8217;s input; let the user correct it</li>
<li>Let the user know how far over the limit he or she is</li>
<li>Provide guidelines even if Javascript is disabled</li>
<li>onBlur and onChange should also trigger the counter, in case the user is pasting text</li>
<li>Keep the counter message simple and clear</li>
</ul>
<p>Another idea I had for a visual counter:<br />
<div id="attachment_116" class="wp-caption alignnone" style="width: 260px"><a href="http://osric.com/chris/accidental-developer/wp-content/uploads/2008/11/counter-bar.png"><img src="http://osric.com/chris/accidental-developer/wp-content/uploads/2008/11/counter-bar.png" alt="Counter - characters remaining" title="counter-bar" width="250" height="20" class="size-full wp-image-116" /></a><p class="wp-caption-text">Counter - characters remaining</p></div><br />
Aside from turning red once past the limit, though I&#8217;m not sure how it would visually convey to the user by how much the text exceeds the limits.</p>
<p><strong>How can a counter be implemented programatically?</strong><br />
I would like to set something up so that the appropriate event handlers are attached to textarea elements automatically. I found a <a href="http://jquery.com/">jQuery</a> plugin, <a href="http://plugins.jquery.com/project/CharLimit">Char Limit</a>, but in addition to not meeting the recommendations I made above, I don&#8217;t like how the developer has to specify the character limit in the Javascript (and apply the same limit for all textarea elements).</p>
<p>I think a clever, although imperfect, solution would be to have jQuery (or any Javascript of your choice) turn any static character limit messages (like the one that Delicious provides if Javascript is disabled) into dynamic character counters. For example, with the following code, the Javascript could find any elements with id attributes matching *_counter, take the numeric part as the limit, and build a character counter:<br />
<code>&lt;textarea name="example" id="example"&gt;&lt;/textarea&gt;<br />
&lt;div id="example_counter"&gt;300 characters maximum&lt;/div&gt;</code></p>
<p>It&#8217;s problematic for several reasons: another element on the page could have an id that matches *_counter, the non-Javascript message could  contain more than one numeric part, and the developers would have be consistent in naming the textareas and accompanying divs. But I think it is in many ways an improvement over the inline Javascript I&#8217;ve seen in some oft-used solutions.</p>
]]></content:encoded>
			<wfw:commentRss>http://osric.com/chris/accidental-developer/2008/11/javascript-textarea-counter/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Users Paste Differently</title>
		<link>http://osric.com/chris/accidental-developer/2008/11/users-paste-differently/</link>
		<comments>http://osric.com/chris/accidental-developer/2008/11/users-paste-differently/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 20:52:50 +0000</pubDate>
		<dc:creator>Chris Herdt</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[textarea]]></category>

		<guid isPermaLink="false">http://osric.com/chris/accidental-developer/?p=103</guid>
		<description><![CDATA[ I&#8217;ve been using a relatively generic Javascript textarea counter for several years to restrict the input length on form textareas. I&#8217;m not sure where the specific version I&#8217;m using came from, but you can find dozens like it on Google.
Almost all of them rely on the onKeyDown and onKeyUp events to trigger the script. [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_104" class="wp-caption alignleft" style="width: 110px"><a href="http://osric.com/chris/accidental-developer/wp-content/uploads/2008/11/paste.jpg"><img src="http://osric.com/chris/accidental-developer/wp-content/uploads/2008/11/paste.jpg" alt="Paste" title="paste" width="100" height="177" class="size-full wp-image-104" /></a><p class="wp-caption-text">Paste!</p></div> I&#8217;ve been using a relatively generic Javascript textarea counter for several years to restrict the input length on form textareas. I&#8217;m not sure where the specific version I&#8217;m using came from, but you can <a href="http://www.google.com/search?q=textarea+counter">find dozens like it on Google</a>.</p>
<p>Almost all of them rely on the <em>onKeyDown</em> and <em>onKeyUp</em> events to trigger the script. However, some users still manage to submit text that exceeded the limits, even though the application required Javascript. I could not for the life of me reproduce this issue&#8230;until today.<br />
<span id="more-103"></span><br />
Naturally, when I tried to enter text into the textarea, I just typed it there. But that&#8217;s not always what users do. Can you imagine, some users might actually compose text in, say, Microsoft Word and then paste it into a textarea? Maybe they are taking advantage of the spell-checker or grammar-checker. Maybe it&#8217;s text that they have composed once, but are submitting to several different sites. Or maybe it&#8217;s some reason I can&#8217;t even fathom!</p>
<p>When I copy-and-paste text, I highlight it, hit CTRL-c, put my cursor at the destination, and hit CTRL-v. But that&#8217;s not always what users do either. And as it turns out, if you right-click and select Paste, neither <em>onKeyDown</em> or <em>onKeyUp</em> are triggered. Adding <em>onBlur</em> and <em>onChange</em> seem to do the trick, though.</p>
<p>Of course, this discovery hasn&#8217;t exactly solved the problem: the script we are using simply truncates text that is too long. In Firefox, if your textarea has a scrollbar, it could show the user just the top portion of his or her pasted text. Everything would look OK, and the character count displayed would show, for example, 500 of 500 characters.</p>
<p>I haven&#8217;t written a replacement script yet, but something clearly needs to alert the user that text has been truncated. At least, however, I have identified the problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://osric.com/chris/accidental-developer/2008/11/users-paste-differently/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
