Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As stated, <code>&lt;textarea maxlength="10"&gt;</code> will work for a small amount of cases - but not <strong>all</strong>.</p> <p>It is hard to determine how to prevent the user (in firefox for example) from entering the text area, selecting the browser menu <code>Edit</code> and then clicking on <code>Paste</code>. I am really at a loss for how to prevent that. You can check the keydown event to prevent <code>ctrl</code> + <code>v</code>. 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).</p> <p>In short, there is no universal way to prevent user paste in a proper fashion. </p> <p>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.</p> <p>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.</p> <p>textarea</p> <pre><code>&lt;textarea id="maintextarea"&gt;&lt;/textarea&gt; </code></pre> <p>script</p> <pre><code>/*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 &gt; cachedText.length) { //TODO: Add additional logic for catching a paste or text change textArea.value = currentText.substr(0, 10); } cacheText(); } </code></pre> <p>I think although this implements a timer, this is a very practical solution. It also happens to work (<em>tested</em>).</p> <p>:)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload