Subversion Repositories My Stuff

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
25 kardasa 1
jQuery.fn.maxLength = function(len)
2
{
3
  var maxLengthKeyPress = new Array();
4
  var maxLengthChange = new Array();
5
  var appleKeyOn = false;
6
 
7
  //the second argument should be true if len should be based on
8
  //the maxlength attribute instead of the input
9
  var useAttr = arguments.length>1 ? arguments[1] : false;
10
 
11
  var handleKeyUp = function(e)
12
  {
13
    //if the apple key (macs) is being pressed, set the indicator
14
    if(e.keyCode==224||e.keyCode==91)
15
      appleKeyOn = false;
16
  }
17
 
18
  var handleKeyDown = function(e)
19
  {
20
    //if the apple key (macs) is being released, turn off the indicator
21
    if(e.keyCode==224||e.keyCode==91)
22
      appleKeyOn = true;
23
  }
24
 
25
  var handleKeyPress = function(e)
26
  {  
27
    //if this keyCode does not increase the length of the textarea value,
28
    //just let it go
29
    if(appleKeyOn || (e.charCode==0&&e.keyCode!=13) || e.ctrlKey)
30
      return;
31
 
32
    //get the textarea element
33
    var textarea = $(this);
34
    //if the length should be based on the maxlength attribute instead of the
35
    //input, use that
36
    len = useAttr ? parseInt(textarea.attr('maxlength')) : len;
37
    //get the value of the textarea
38
    var val = textarea.val();
39
    //get the length of the current text selection
40
    var selected = Math.abs(textarea.attr('selectionStart') - textarea.attr('selectionEnd'));
41
    selected = isNaN(selected) ? 0 : selected;
42
    //if this is the maximum length
43
    if(val.length==len && selected<1)
44
      return false;
45
    else if(val.length>len && selected<(val.length-len))
46
      return textarea.val(textarea.val().substring(0,len));
47
  };
48
 
49
  var handleChange = function(e)
50
  {
51
    //get the textarea element
52
    var textarea = $(this);
53
 
54
    //if the length should be based on the maxlength attribute instead of the
55
    //input, use that
56
    len = useAttr ? parseInt(textarea.attr('maxlength')) : len;
57
 
58
    //truncate the textarea to its proper length
59
    textarea.val(textarea.val().substring(0,len));
60
  };
61
 
62
  //get the current keyup and change functions
63
  var removeKeyPress = maxLengthKeyPress[this.selector];
64
  var removeChange = maxLengthChange[this.selector];
65
 
66
  //remove the keyup and change functions from any matched elements in case
67
  //a maxlength was previously set and a new one is being set
68
  this.die('keypress', removeKeyPress);
69
  this.die('change', removeChange);
70
 
71
  if(len==0 && !useAttr)
72
    return;
73
 
74
  //set the keyup and change functions for this element set and all future
75
  //elements matching this selector
76
  this.live('keypress', handleKeyPress);
77
  this.live('change', handleChange);
78
  this.live('keydown', handleKeyDown);
79
  this.live('keyup', handleKeyUp);
80
 
81
  //save the current keyup and change functions so that they can be
82
  //remove later
83
  maxLengthKeyPress[this.selector] = handleKeyPress;
84
  maxLengthChange[this.selector] = handleChange;
85
 
86
  //trigger a keypress event so that the limit will be enforced
87
  this.keypress();
88
};
89
 
90
$(function()
91
{
92
  //limit the length of all textareas with the maxlength attribute
93
  //NOTE: setting maxlength on a textarea is NOT valid XHTML. this
94
  //was added for coders who want to code loosely--not me!
95
  $('textarea[maxlength]').maxLength(null, true);
96
});