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

一曲冷凌霜 提交于 2020-01-16 14:02:12

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/18548122/how-to-disable-ctrlv-paste-function-in-a-jsf-page-i-am-using-primefaces-co

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!