jQuery Textarea NobleCount Plugin Addition - Prevent Paste

Javascript , jQuery Add comments

Firstly, thanks to Jeremy Horn A.K.A. The Product Guy for the jQuery NobleCount plugin.

I've had to use it recently and found it to be quite good, so I've decided to contribute back also.  Nothing much but a possible addition for preventing users from pasting charaters into the relevant textarea and bypassing the keyboard events. See below:

Prevent Pasting

$(t_obj).bind('paste', function(e) {
    var textArea = $(t_obj);
        function() {
            var text = textArea.val();                       
            // If you wish to implement a check before the text area is populated 
            // you can use window.clipboardData.getData("Text").length
            // The only side effect is that this is only available in IE (as far as I'm aware).
            if (text.length > max_char) {
            // Concatenate the textarea text to that of the allowable length.
            textArea.val(text.substr(0, max_char));
            alert('Maximum length exceeded: ' + max_char + ' \n\nYour text will be automatically truncated to ' + max_char + ' Characters.');
            // Reset the counter details.
            event_internals(t_obj, char_area, c_settings, max_char, true);
            return false;
           }, 100);

This should be placed directly below the $(t_obj).keyup() method in the plugin for now.  Implementation examples for the plugin can be found here.

I have contacted Jeremy, and hopefully this or a preferrable version will be added to the main plugin in the near future.

Feel free to modify \ improve to suit, but always check Jeremys site for the latest version and updates. 

For convenience and example, I have included my version for download here.

Bookmark and Share

2 responses to “jQuery Textarea NobleCount Plugin Addition - Prevent Paste”

  1. apad Says:
    Thanks for the great insight u have. Solve my headache with people copy and paste, then arguing half of the data was truncated.
  2. Prajwol Says:
    Thx.. buddy :)

Leave a Reply

Leave this field empty:

Powered by Mango Blog. Design and Icons by N.Design Studio