您现在的位置: 365建站网 > 365文章 > 表单中禁用TextBox和Input框的粘贴功能

表单中禁用TextBox和Input框的粘贴功能

文章来源:365jz.com     点击数:393    更新时间:2011-01-25 20:52   参与评论

在.net程序系统中有时需要对文本框的输入做一些限定,比如禁用粘贴功能。

一般对文本框禁用快捷键Ctrl+C的粘贴功能以及右键的粘贴功能可试用JS代码实现。

可参考:

</>code

  1. function click(e)
  2. {
  3. if (document.all)
  4. {
  5. if (event.button==1||event.button==2||event.button==3)
  6. {
  7. oncontextmenu='return false';
  8. }
  9. }
  10. if (document.layers)
  11. {
  12. if (e.which == 3)
  13. {
  14. oncontextmenu='return false';
  15. }
  16. }
  17. }
  18. if (document.layers)
  19. {
  20. document.captureEvents(Event.MOUSEDOWN);
  21. }
  22. document.onmousedown=click;
  23. document.oncontextmenu = new Function("return false;")
  24. var trxdyel=true
  25. var hotkey=17 /* hotkey即为热键的键值,是ASII码,这里99代表c键 */
  26. if (document.layers)
  27. document.captureEvents(Event.KEYDOWN)
  28. function gogo(e)
  29. {
  30. if (document.layers)
  31. {
  32. if (e.which==hotkey && trxdyel)
  33. {
  34. alert('操作错误.或许是您按错键了!');
  35. }
  36. }
  37. else if (document.all)
  38. {
  39. if (event.keyCode==hotkey&&trxdyel){ alert('操作错误.或许是您按错键了!'); }}
  40. }
  41. document.onkeydown=gogo

将以上JS代码写到JS文件中取名为xp.js并放入Script文件夹中,引用时需要注意设置Charset=“gb2312”,不然提示出的信息会是乱码。页面引用:
<script src="../Script/xp.js" type="text/javascript" charset="gb2312"></script>

实际上禁用文本框的粘贴功能系统提供了一个完美的解决方案。试用onpaste属性,让其返回假即可禁用此文本框的粘贴功能。

代码为:onpaste="return false;" 

示例:input

<input onpaste="return false;" type="text" name="textfield" >

或者TextBox

<asp:TextBox ID="name" runat="server" onpaste="return false;" ></asp:TextBox>

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (393人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号