0

I am using jQuery.

Objective:

  • I am disabling any used input if the max length is reached for a textarea.
  • I am handling keyDown event and cancelling it.

Problem:

  • The user may want to delete the last word.

Here is a snippet:

editTweet.keydown(function(e) {
    // Can i check if the (e) is going to increase the text
    var left = Limit - editTweet.val().length;
    if (left <= 0) {
        return false;
    }
    spn.text(left);
});
TheHippo
  • 61,720
  • 15
  • 75
  • 100
Moons
  • 3,833
  • 4
  • 49
  • 82
  • so why not using keyup event instead and 'substring' it if length is over ? – A. Wolff Jan 03 '14 at 14:08
  • Two ways, either check to see the keystroke (del/backspace etc), or change this to keyup, and the val().length will then reflect the intended change, then you can just remove whatever was added (if not deleting) – ginman Jan 03 '14 at 14:08
  • you can capture the delete or backspace like the [answer here](http://stackoverflow.com/questions/664631/disable-backspace-and-delete-key-with-javascript-in-ie) – Pete Jan 03 '14 at 14:09

1 Answers1

0

Solved it using:

 if (e.which < 0x20) {
        // e.which < 0x20, then it's not a printable character
        // e.which === 0 - Not a character
        return;     // Do nothing
    }

Copied from Here

Community
  • 1
  • 1
Moons
  • 3,833
  • 4
  • 49
  • 82