Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > javascript: limit textarea input length. problem.

javascript: limit textarea input length. problem.
Thread Tools
Mac Enthusiast
Join Date: Nov 2001
Location: Washington, DC 20009
Status: Offline
Reply With Quote
Apr 27, 2005, 12:40 PM
 
I have a requirement to limit all textareas to 1000 characters to prevent issues with a varchar(1000) in our oracle database table. I've found enough Javascript code to piece together a function that limits the size. Here is what I have so far.

Code:
function limitText(field, count, limit) { if (field.value.length > limit) field.value = field.value.substring(0, limit); else count.value = limit - field.value.length; }
This works to stop the user from input more that 1000 characters. My problem is this: in Netscape, when the user hits the 1001st character, the function takes action to truncate the text, just as it should. All is fine and dandy except the cursor ends up at the first character position. If the user continues to type, unknowing of the 1000 character max, the input begins to be overwritten from the beginning. On IE, this problem does not occur, as the cursor remains at the end of the input string.

Does anyone have a work-around for this? Something to force the cursor to the end would probably be the most straightforward solution. TIA.
Just my $.02 :-)
Ti Powerbook 1Ghz w/ Superdrive ......and lovin' it! :)
     
Clinically Insane
Join Date: Nov 1999
Status: Offline
Reply With Quote
Apr 27, 2005, 01:36 PM
 
I don't know of a way to do force the cursor to the end, but might you be able to use the alert() function to put up a dialog box indicating that they've reached the limit? This would trap the next few keystrokes the user tried to input, such that this wouldn't be a problem.
You are in Soviet Russia. It is dark. Grue is likely to be eaten by YOU!
     
milf  (op)
Mac Enthusiast
Join Date: Nov 2001
Location: Washington, DC 20009
Status: Offline
Reply With Quote
Apr 28, 2005, 11:48 AM
 
Originally Posted by Millennium
I don't know of a way to do force the cursor to the end, but might you be able to use the alert() function to put up a dialog box indicating that they've reached the limit? This would trap the next few keystrokes the user tried to input, such that this wouldn't be a problem.
I attempted this approach, but Netscape doesn't even seem to acknowledge the alert(). I removed the bit of code to truncate so that it just notifies the user when he/she has reached 1000 characters.

Code:
if (field.value.length > limit) { alert("You've reached the" + limit + " character limit."); }
This works fine with IE, but does nothing under Netscape. What gives?
Just my $.02 :-)
Ti Powerbook 1Ghz w/ Superdrive ......and lovin' it! :)
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:27 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2