Nithin Viswanathan
Nithin Viswanathan

Reputation: 3283

How to restrict paste function after the maxlength

I have got a task to restrict paste function when it comes to maximum. For that I used the code

 $(document).on('keyup', '.txtStyle', function (e) {
                debugger;
                var charsSoFar = $('#' + container_id).val().length;
                remaining = 500 - charsSoFar;
                $('#rem_' + container_id).text('Characters remaining ' + remaining);
                if (charsSoFar > 500) {
                    alert("Hai");
                }
            });

How can I restrict the Paste function? Please help

Upvotes: 1

Views: 413

Answers (3)

Neha
Neha

Reputation: 1548

As my understanding you want to stop user input once its reach max, so i suggest

if (textbox.val().length >500 ) 
   textbox.val(textbox.val().substring(0,500)); 

It will give illusion that after 500 char user can't input or paste in textbox.

Upvotes: 2

Kunj
Kunj

Reputation: 2018

$(document).on('keyup', '.txtStyle', function(e) { debugger;
    var charsSoFar = $('#' + container_id).val().length;
    remaining = 500 - charsSoFar;
    $('#rem_' + container_id).text('Characters remaining ' + remaining);
    if (charsSoFar > 500) {
        if (e.keyCode == 86 && e.ctrlKey) {
            alert("Hai");
        }
    }
});

Upvotes: 0

Illaya
Illaya

Reputation: 666

Take look on this..... This will help you.

if (event.ctrlKey==true && (event.which == '118' || event.which == '86')) {
   alert('Paste Restricted!');
   e.preventDefault();
}

Demo : http://jsfiddle.net/6Gdkk/

Upvotes: 0

Related Questions