/docs/MyDocs

To get this branch, use:
bzr branch http://darksoft.org/webbzr/docs/MyDocs

« back to all changes in this revision

Viewing changes to Development/languages/js/textarea.txt

  • Committer: Suren A. Chilingaryan
  • Date: 2009-04-09 03:21:08 UTC
  • Revision ID: csa@dside.dyndns.org-20090409032108-w4edamdh4adrgdu3
import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
How do I stop the user from entering more than x number of chars in a textarea
 
2
==============================================================================
 
3
Internet Explorer 4 and Netscape Navigator 4 can capture keystrokes but have
 
4
problems if the user presses backspace or so...
 
5
 
 
6
I would try this - the onKeyUp counts the strokes in v4 and the onChange
 
7
does the dirty deed on v3:
 
8
 
 
9
<script language="JavaScript"><!--
 
10
maxKeys = 100;
 
11
keysSoFar = 0;
 
12
alerted = false;
 
13
 
 
14
function change(what) {
 
15
    if (!alerted) alert('Keep it short, please');
 
16
    what.value = what.value.substring(0,maxKeys-1); // chop after 100
 
17
    alerted = true;
 
18
}
 
19
 
 
20
function keyup(what) {
 
21
    keysSoFar++;
 
22
    if (keysSoFar > maxKeys) {
 
23
        if (!alerted) alert('That\'s enough!');
 
24
        what.value = what.value.substring(0,maxKeys-1); // chop the last
 
25
typed char
 
26
        alerted = true;
 
27
    }
 
28
}
 
29
//--></script>
 
30
 
 
31
<form>
 
32
<textarea cols=100 rows=20 onChange="change(this)"
 
33
onKeyUp="keyup(this)"></textarea>
 
34
</form>