user2197806
user2197806

Reputation: 7

How to disable ctrl+v (paste) function in a JSF page ?. I am using primefaces component.

I have an inputText component where i am accepting only integer values by validating through keyup event. but i am not able to disable ctrl+v option so the user is able to paste string value into the input text box. html has onpaste option but JSF does not! Thanks in advance.

Upvotes: 0

Views: 5879

Answers (1)

noone
noone

Reputation: 19776

With JSF 2.2 you can "pass through" attributes to be rendered via <f:passThroughAttribute /> and you would be able to add something like onpaste="return false;", but I assume you do not use JSF 2.2 yet.

You can still easily disable copy-pasting via some custom javascript. With standard javascript this is as easy as this (taken from here):

window.onload = function() {
   var myInput = document.getElementById('myInput');
   myInput.onpaste = function(e) {
       e.preventDefault();
   }
}

With jQuery you can do it like this (taken from here):

$(document).ready(function(){
    $('#myInput').bind("cut copy paste",function(e) {
        e.preventDefault();
    });
});

The jQuery option is also nice, because you can easily do this with all inputs on your site using a combination of the above code and $('input').each

Upvotes: 3

Related Questions