Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 1st, 2007, 03:05 PM
Registered User
 
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to kennethlove
Default Textarea Limiter problems

Hey all. I followed the guide in the "Professional Javascript for Web Developers" book to create a maxlength'd textarea, and it works. You can only put in a set number of characters. The problem comes when you've reached the limit and want to delete some of your characters. Apparently Javascript is picking up the delete/backspace key presses and blocking them.

So, my question is how to unblock those keys?

oh, and for those not familiar with the example, here's what I'm using:
Code:
var TextUtil = new Object();
      TextUtil.isNotMax = function(oTextArea) {
        return oTextArea.value.length != oTextArea.getAttribute("maxlength") - 1;
      }
Which gets fired off onkeypress.
 
Old May 1st, 2007, 06:28 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

Here is one that works very well, i use it alot. Its cut n paste and gives an example of how to use multiple char limit text areas on a page, very useful...

<html>
 <head>
  <title>Untitled</title>
   <SCRIPT LANGUAGE="JavaScript">
   function textCounter(field,cntfield,maxlimit)
   {
      if (field.value.length > maxlimit)
         field.value = field.value.substring(0, maxlimit);
      else
         cntfield.value = maxlimit - field.value.length;
   }
</script>
</head>
<body>
<form name="myForm"><textarea name="message1" wrap="physical" cols="28" rows="5" onKeyDown="textCounter(document.myForm.message1,do cument.myForm.remLen1,125)" onKeyUp="textCounter(document.myForm.message1,docu ment.myForm.remLen1,125)"></textarea>
<br>
<input readonly type="text" name="remLen1" size="3" maxlength="3" value="125">
chars left
<br>
<textarea name="message2" wrap="physical" cols="28" rows="5" onKeyDown="textCounter(document.myForm.message2,do cument.myForm.remLen2,125)" onKeyUp="textCounter(document.myForm.message2,docu ment.myForm.remLen2,125)"></textarea>
<br>
<input readonly type="text" name="remLen2" size="3" maxlength="3" value="125">
chars left
<br>
</form>
</body>
</html>

Wind is your friend
Matt
 
Old May 2nd, 2007, 01:20 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I think you need to test the keyCode, if it's 8 (backspace) then skip the rest of the function. There maybe other keyCodes you need to test for such as delete and the arrow keys.

--

Joe (Microsoft MVP - XML)
 
Old May 2nd, 2007, 10:58 AM
Registered User
 
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to kennethlove
Default

joefawcett actually hit the same solution I did. Tested for the keypress, if it was 8 (backspace) or 46 (delete), then I let the keystroke through, otherwise, if the limit has been reached, the key is blocked. I figured it out about 2 minutes after posting, in typical self-help-desk fashion.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Tab function + <textarea></textarea> Adam H-W HTML Code Clinic 0 April 24th, 2007 10:36 AM
display vbCrLf's from textarea to textarea mat41 Classic ASP Basics 8 June 10th, 2004 12:19 AM
TextArea microchip Classic ASP Databases 2 March 17th, 2004 11:19 AM
Textarea and Replace problems devplus Classic ASP Professional 2 February 25th, 2004 04:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.