How to disable paste in textfield

喜你入骨 提交于 2019-12-25 04:07:28

问题


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

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