问题
I have 2 textfield, the first for the email and the second is used to confirm the email. The user must not be able to paste (ctrl+v or with mouse) anything in the confirm s:textfield of struts 2.
All the examples that I founded use only simple html, can I use a textfield of Struts 2?
回答1:
The problem of the other examples is that all was based on "onPaste" event in the html edit text, and the textfield of Struts2 doesn't have this event.
That is not a problem for Struts UI Tags that have :
Dynamic Attributes Allowed: true
<s:textfield/>
, along with <s:file/>
, <s:textarea/>
and others, has it, as you can read in the documentation (under Parameters).
This means that everything you write in the tag (that is not a Struts UI Tag attribute) will be reported in the generated HTML as-is, letting you use new features without forcing Apache to release new versions of the Tags.
回答2:
Ok, I found by myself the solution, in the jsp:
<s:textfield name="confermaEmail" id="idConfermaEmail" size="30" onFocus="disablePaste()"/>
And I used a javascript:
function disablePaste(){
var input = document.getElementById("idConfermaEmail");
if (input)
input.onpaste = function(){return false;};
}
I tried this on IE 10 and Chrome.
However like @Andrea Ligios said in another comment, it's also possible to use the onPaste event on struts 2.
Edit: It's better to use onFocus instead onPaste because the event of onPaste block the "pasting" only after the first paste. Instead onFocus block the "pasting" just on the focus, so before of the first paste.
来源:https://stackoverflow.com/questions/21255632/how-to-disable-paste-in-textfield