Character Counting question 
Author Message
 Character Counting question

I have a text area and I would like to limit my users to 300 characters.  Could someone suggest a function that would not allow a user to input anything after the 300th character.  Any leads would be greatly appreciated.

Thanks in advance.

Katie Smith



Sun, 08 Jun 2003 04:28:15 GMT  
 Character Counting question

Handle the onkeypress event and check the length of the textarea's innerText property.  If it is less than 300, do nothing otherwise assigning "window.event.keyCode = 0" will "eat" the character.  Be advised though that pasting text in with a menu command won't fire a onkeypress.  Also, the Enter key doesn't a 2 for the crlf pair unless another character is actually entered You'll still need to check the length in the onblur for the textarea or in the onsubmit of the form it's contained in.

This simple example does NOT check in the onblur nor does it even enclose the textarea in a form, but you get the idea and it's something easy to experiment with...

<html>
<head>
<script language=VBScript>
sub foo_onkeypress()
  if Not (len(foo.innerText) < 10) then
    window.event.keyCode = 0
    exit sub
  end if
  setTimeout "msg.innerText = ""length="" & len(foo.innerText)",0
end sub
</script>
<body>
<textarea id=foo rows=20></textarea>
<div id=msg></div>
</body>
</html>

--
Michael Harris
Microsoft.MVP.Scripting
--

Please do not email questions - post them to the newsgroup instead.
--


  I have a text area and I would like to limit my users to 300 characters.  Could someone suggest a function that would not allow a user to input anything after the 300th character.  Any leads would be greatly appreciated.

  Thanks in advance.

  Katie Smith



Sun, 08 Jun 2003 11:57:15 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Counting characters

2. procedure to count characters

3. Counting the number of occurrences of a character...

4. return the count of a particular character in a string

5. count number of characters in a string

6. running character count

7. Counting characters as they are typed

8. counting characters in a string

9. COUNT OF CHARACTERS IN A MAIL

10. How to count the Reference Characters ?

11. Count occurances of a character string?

12. Ignore strikethrough text in document character count

 

 
Powered by phpBB® Forum Software