不允许粘贴任何非字母数字字符
问题描述:
我不希望用户允许在文本框中粘贴任何非字母数字字符。
如何在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.