问题
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