不允许粘贴任何非字母数字字符 [英] Do not allow Paste any non alphanumeric characters

查看:78
本文介绍了不允许粘贴任何非字母数字字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不希望用户允许在文本框中粘贴任何非字母数字字符。
如何在Javascript中限制此项?
谢谢!!

I don’t want user to allow pasting of any non Alphanumeric characters on a text box. How do I restrict this in Javascript? Thanks!!

推荐答案

您可以使用 onblur 文本框的事件。

You can use the onblur event of text box.

function remove()
{
  var otxt=document.getElementById('txt1'); 

var val=otxt.value;

 for(i=0;i<val.length;i++)
   {
     var code=val.charCodeAt(i);
     if(!(code>=65 && code<=91) && !(code >=97 && code<=121) && !(code>=48 && code<=57))
         { otxt.value=""; return ; }    

   }
}


<input type="text" id="txt1" onblur="remove();" />

当您输入非字母数字值时,它将删除文本框的所有值。

It will remove all value of text box when you input non alphanumeric value.

这篇关于不允许粘贴任何非字母数字字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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