Is it possible to have a HTML input on a form that only accepts pasted input?

是否可能有一个只接受粘贴输入的表单上的HTML输入?

As part of our registration process, the enduser needs to enter a 20 character identification token into a basic HTML input form. Ideally, the user would simply copy/paste the token into the appropriate field. We don't want to allow the user to manually type this in, as it is likely that they will mistype letters, and we don't have any reliable means to validate the input.

作为注册过程的一部分,最终用户需要在一个基本的HTML输入表单中输入一个20个字符标识符。理想情况下,用户只需将令牌复制/粘贴到适当的字段中。我们不希望用户手动输入,因为很可能他们会错写字母,而且我们没有任何可靠的方法来验证输入。

The token comes from desktop software and needs to be pasted into an already opened webpage (i.e. where they download the software from). As such, a clickable link isn't a viable option.

该令牌来自桌面软件,需要粘贴到已打开的网页(即下载软件的地方)。因此,点击链接并不是一个可行的选择。

Is there any way to do this, e.g. through Javascript? Thanks.

有没有办法做到这一点,例如通过Javascript?谢谢。


My solution, adapted from SimplePi's answer:

我的解决方案,改编自SimplePi的回答:

<html>
<body>
<script type="text/javascript">
 function validate(evt) {
   var theEvent = evt || window.event;
   var key = theEvent.charCode || theEvent.which;

   if(key >= 32 && (theEvent.ctrlKey === undefined || !theEvent.ctrlKey)) {
    if(theEvent.preventDefault) theEvent.preventDefault();
    else theEvent.returnValue = false;
  }
 }
 </script>
  <span>Textbox name</span> <br />
 <input type="text" onkeypress='validate(event)' value=""/>
</body>
</html>

This works in some, but not all browsers. Firefox on Mac is the only offender I've found - firefox in general reports ctrl-v the exact same as v, but on windows the theEvent.ctrlKey member can differentiate between the two. Doing the same on a mac will apparently require keydown/up to check whether or not cmd is pressed. It's doable, but not included in this code, in case anyone else wishes to use this.

这在某些浏览器中可以工作,但不是所有浏览器都可以。Mac上的Firefox是我发现的唯一的违法者——Firefox一般报告ctrl-v与v完全相同,但在windows上则是theEvent。ctrl键成员可以区分两者。在mac上做同样的操作显然需要按下/向上键来检查是否按下cmd。它是可行的,但不包括在代码中,以防其他人希望使用它。

3 个解决方案

#1


5

<script type="text/javascript">
 function validate(evt) {
   var theEvent = evt || window.event;
   var key = theEvent.keyCode || theEvent.which;
   key = String.fromCharCode(key);
  var regex = /[]|\./;
   if(!regex.test(key)) {
    theEvent.returnValue = false;
    if(theEvent.preventDefault) theEvent.preventDefault();
  }
 }
 </script>
  <span>Textbox name</span>
 <input name="ReceiveNo" type="text" class="txtbox" onkeypress='validate(event)' value=""         required tabindex="" />

更多相关文章

  1. 9.1.4 前端 - HTML body标签 - 标题,段落,分割线,换行,特殊符号,列
  2. html5: 新特性(表单)
  3. 如何使用JavaScript验证此HTML表单?
  4. Jquery - 表单验证,为错误消息添加css样式
  5. 将文本从表单复制到另一个网站的文本字段
  6. J2EE进阶之onsubmit表单提交 五
  7. jquery入门-$.each 数组操作与表单操作代码
  8. 的良好实践是什么?它可以替换还是只用于表单?
  9. 【ASP.NET Web API教程】5.2 发送HTML表单数据:URL编码的表单数据

随机推荐

  1. 基于业务和平台理解数字营销概念
  2. 打卡学习
  3. Plotly中4种文本类型设置详解
  4. 裸辞,杀回一线!
  5. Ansible 之 自动化部署redis主从(单机)
  6. 小鹿又熬肝写了一份 Vue 2.0 核心原理!
  7. 《Python知识手册》更新V2.2版,添加 Plotl
  8. TensorFlow 实战多元线性回归问题
  9. 小鹿 | 谈谈我的三观!
  10. 深度学习中几种常用框架的介绍