2

The code below is working fine except that if I paste a string with more than 10 characters in it, it should only paste in the first 10 characters. Currently it does not do this, how do I prevent more than 10 characters being pasted?

http://jsfiddle.net/qfzkw/2/

Alex Moore
  • 3,415
  • 1
  • 23
  • 39
Nick Kahn
  • 19,652
  • 91
  • 275
  • 406

3 Answers3

2

As stated, <textarea maxlength="10"> will work for a small amount of cases - but not all.

It is hard to determine how to prevent the user (in firefox for example) from entering the text area, selecting the browser menu Edit and then clicking on Paste. I am really at a loss for how to prevent that. You can check the keydown event to prevent ctrl + v. You can disable the context menu to disallow the right clicking of the text area (although a malicious user can edit the front end javascript and then re-enable this feature).

In short, there is no universal way to prevent user paste in a proper fashion.

However, you could hack a solution (always my favorite when it seems there is no way to continue on something you really want). This approach I am about to suggest is dependent on how many timers you have running. If you have multiple timers for animation, google suggests you try to fold them all into one timing unit which the others derive from. If this is the case, please refactor your timers.

Implement a global timer. Implement a function which runs ever 25ms based on that timer. Cache the contents of the textarea. See if those contents have changed.

textarea

<textarea id="maintextarea"></textarea>

script

/*init*/
var globalTimer = new Date();
var cachedText = document.getElementById("maintextarea").value;
var iterationCount = 0;

function cacheText() {
    cachedText = document.getElementById("maintextarea").value;
}

function upDateTimer() {
    globalTimer = new Date();
    var timerTimeout = setTimeout('upDateTimer()', 5);
    timeBasedOperations();
}
upDateTimer();

function timeBasedOperations() {
    iterationCount++;//this will allow timing to occur
    //TODO: Add in other timers calls based on global timer
    if (iterationCount % 5) {//every fifth iteration (hence 25ms)
        checkTextArea();
    }
}

function checkTextArea() {
    var currentText = document.getElementById("maintextarea").value;
    var textArea = document.getElementById("maintextarea");
    if (currentText.length > cachedText.length) {
        //TODO: Add additional logic for catching a paste or text change
        textArea.value = currentText.substr(0, 10);
    }
    cacheText();
}

I think although this implements a timer, this is a very practical solution. It also happens to work (tested).

:)

Travis J
  • 81,153
  • 41
  • 202
  • 273
1

simply use <textarea maxlength="10"></textarea>​

No javascript needed for this. It automatically takes care of copy paste also.

Here is the updated jsFiddle.

gopi1410
  • 6,567
  • 9
  • 41
  • 75
  • have you test it? i did try with maxlength and i still can able to paste more then allowed characters... i tested the jsfiddle you modified but still the same – Nick Kahn Apr 30 '12 at 20:19
  • @AbuHamzah Yup, the above jsFiddle works fine even when pasting characters >10 in length. I just checked it. – gopi1410 Apr 30 '12 at 20:23
  • try this, copy the text that have more then 10 characters and paste – Nick Kahn Apr 30 '12 at 20:23
  • @AbuHamzah What browser and version are you using? – Alex Moore Apr 30 '12 at 20:28
  • In firefox, this will not prevent the pasting of more than 10 characters. In chrome it works. – Travis J Apr 30 '12 at 20:34
  • i am trying with FF(8) and IE (8) – Nick Kahn Apr 30 '12 at 20:39
  • @Gopi: there still bug in my code and if you backspace the characters that does not count(reflect) unless you press the space bar, have you noticed that? – Nick Kahn Apr 30 '12 at 20:41
  • @AbuHamzah yeah, for that you have to call the function calculate or whatever you are using to count the characters on the `onfocus` event or `keyup` event, so that it is called automatically when something is pasted. – gopi1410 Apr 30 '12 at 20:45
  • & as Alex said this question possibly provides a cross browser solution. http://stackoverflow.com/questions/1125482/how-to-impose-maxlength-on-textarea-in-html-using-javascript – gopi1410 Apr 30 '12 at 20:51
  • maxlength attribute on textarea will not work on IE8, IE9. It is only supported from IE10 onwards. – Blake Jun 07 '13 at 06:32
0

You can't peek into the clipboard buffer from browsers, so your only option is to limit it to 10 chars like you currently do. Use the maxlength="10" attribute instead of the javascript character counting thing you got going on.

<textarea maxlength="10"></textarea>​
Alex Moore
  • 3,415
  • 1
  • 23
  • 39