如何禁用文本字段中的粘贴 [英] How to disable paste in textfield

查看:38
本文介绍了如何禁用文本字段中的粘贴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个文本字段,第一个用于电子邮件,第二个用于确认电子邮件.用户不得在 struts 2 的确认 s:textfield 中粘贴(ctrl+v 或使用鼠标)任何内容.

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.

我创建的所有示例都只使用简单的html,我可以使用Struts 2的文本字段吗?

All the examples that I founded use only simple html, can I use a textfield of Struts 2?

推荐答案

好的,我自己找到了解决方案,在jsp中:

Ok, I found by myself the solution, in the jsp:

<s:textfield name="confermaEmail" id="idConfermaEmail" size="30"  onFocus="disablePaste()"/>

我使用了 javascript:

And I used a javascript:

function disablePaste(){
    var input = document.getElementById("idConfermaEmail");
    if (input)
       input.onpaste = function(){return false;};
}

我在 IE 10 和 Chrome 上试过了.

I tried this on IE 10 and Chrome.

不过,就像@Andrea Ligios 在另一条评论中所说的那样,也可以在 struts 2 上使用 onPaste 事件.

However like @Andrea Ligios said in another comment, it's also possible to use the onPaste event on struts 2.

最好使用 onFocus 而不是 onPaste 因为 onPaste 事件仅在第一次粘贴后才会阻止粘贴".相反,onFocus 只是在焦点上阻止粘贴",所以在第一次粘贴之前.

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.

这篇关于如何禁用文本字段中的粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆