如何仅为字母字母启用onpaste [英] How to enable onpaste only for alphabetic letters

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

问题描述

我有一个任务来禁止粘贴文本框。只有字母字符必须允许粘贴。
我的代码是

 < b>名称< / b> 
< input type =textid =nameonpaste =return false/>

通过给onpaste =return false,不粘贴任何值。
如何解决这个问题?

解决方案

试试这段代码

(pre> $(。alphabetOnly)。bind('paste',function(e){
var self = this;
setTimeout(function(e ){
var val = $(self).val(); $ b $ if(val!='0'){
if(val.match(/ ^ [0-9] + ($。)!= null){
$(。alphabetOnly)。val();
}
$(this).val(val);
}
},0);
});

我更新了代码这里


I have got a task to disable the textbox from pasting.Only the alphabetic characters must be allow to paste. My code is

<b>Name</b>
<input type="text" id="name" onpaste="return false"/>

By giving onpaste="return false" no values are pasted. How can I solve this problem?

解决方案

Try this code

$(".alphabetOnly").bind('paste', function(e) {
  var self = this;
  setTimeout(function(e) {
    var val = $(self).val();
    if (val != '0') {
      if (val.match(/^[0-9]+$/) != null) {
        $(".alphabetOnly").val("");
      }
      $(this).val(val);
    }
  }, 0);
});

I have updated the code here

这篇关于如何仅为字母字母启用onpaste的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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