Javascript 如何禁止在文本框中粘贴特殊字符

Javascript 如何禁止在文本框中粘贴特殊字符,javascript,jquery,html,Javascript,Jquery,Html,如何禁止在文本框中粘贴特殊字符 Im使用onkeypress事件处理程序 function disableOtherChar(evt) { var charCode; charCode = (evt.which) ? evt.which : evt.keyCode; var ctrl; ctrl = (document.all) ? event.ctrlKey : evt.modifiers & Event.CONTROL_MASK; if ((c

如何禁止在文本框中粘贴特殊字符

Im使用onkeypress事件处理程序

function disableOtherChar(evt) {
    var charCode;
    charCode = (evt.which) ? evt.which : evt.keyCode;
    var ctrl;
    ctrl = (document.all) ? event.ctrlKey : evt.modifiers & Event.CONTROL_MASK;
    if ((charCode > 47 && charCode < 58) || (charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123) || charCode == 8 || charCode == 9 || charCode == 45 || (ctrl && charCode == 86) || ctrl && charCode == 67) {
        return true;
    } else {
        $(":text").live("cut copy paste", function (e) {
            e.preventDefault();
        });
        return false;
    }
}
函数disableOtherChar(evt){
var字符码;
charCode=(evt.which)?evt.which:evt.keyCode;
var-ctrl;
ctrl=(document.all)?event.ctrlKey:evt.modifiers和event.CONTROL\u掩码;
如果((charCode>47&&charCode<58)| |(charCode>64&&charCode<91)| |(charCode>96&&charCode<123)| charCode==8 | | charCode==9 | | charCode==45 | |(ctrl&&charCode==86)| ctrl&&charCode==67){
返回true;
}否则{
$(“:text”).live(“剪切复制粘贴”,函数(e){
e、 预防默认值();
});
返回false;
}
}

但粘贴时它不会阻止特殊字符,只会在输入时阻止,而不是回答,只是关于以下内容的注释:

var ctrl;
ctrl = (document.all) ? event.ctrlKey:evt.modifiers & Event.CONTROL_MASK;
请学习使用特征检测,基于对象推断的推断行为至少在某些时候注定会失败

另外,不要使用密钥代码,测试实际字符。例如,如果您只想允许字母、数字和少量其他内容:

function hasInvalidChars(s) {

  // allow letters, spaces, numbers only
  var validChars = /[\w\s\d]/gi;
  var x = s.replace(validChars, '');
  return !!x.length;
}

alert(hasInvalidChars('asdf1234 1234asd'));  // false
alert(hasInvalidChars('asdf1.234 1234asd')); // true
将有效字符集扩展为所需的任意字符

哦,如果你想让它成为一行:

function hasInvalidChars(s) {
  return !!s.replace(/[\w\s\d]/gi, '').length;
}

假设您有一个输入


您可以使用以下脚本来验证副本:

$(函数(){
$(“#textInput”).bind('paste',function()
{
setTimeout(函数()
{ 
//获取输入文本的值
var data=$('#textInput').val();
//将特殊字符替换为“”
var dataFull=data.replace(/[^\w\s]/gi',);
//设置不带特殊字符的输入文本的新值
$('#textInput').val(dataFull);
});
});
});

您可以使用第三方插件,例如,它也适用于粘贴(ctrl+v)。 代码如下所示:

$("#elemet").alphanum({ allow : "asd", disallow : "!@#", allowUpper : false });
$(“输入”).alphanum()

或者你可以用一种更专业的方式来使用它,比如:

$("#elemet").alphanum({ allow : "asd", disallow : "!@#", allowUpper : false }); $(“#elemet”).alphanum({ 允许:“asd”, 不允许:“!@#”, allowUpper:错误 }); 上面的代码需要添加到JQuery声明中


我提到的事实是,您还可以从第124行的脚本
jquery.alphanum.js
中修改黑名单数组。您将找到一个函数名getBlacklistAscii,在这里您可以修改
var blacklist=…
,使之适合您。

我对Christian提供的脚本做了一些修改

此版本将所有不在空格(ASCII DEC 32)到波浪号(ASCII DEC 126)和空白字符之间的字符都替换掉。这意味着所有不可见的字符都应该删除

如果在Jquery环境中添加类api_clean_字符,这应该是现成的

<textarea class="api_clean_characters"></textarea>


$(function(){

    $( ".api_clean_characters" ).bind( 'paste',function(ev)
    {
        var $target = $(ev.target);
        setTimeout(function()
        {
            //get the value of the input text
            var data= $target.val() ;
            //replace the special characters to ''
            var dataFull = data.replace(/[^ -~\s]/gi, '');
            //set the new value of the input text without special characters
            $target.val(dataFull);
        });

    });
});

$(函数(){
$(“.api\u clean\u characters”).bind(‘粘贴’,函数(ev)
{
变量$target=$(ev.target);
setTimeout(函数()
{
//获取输入文本的值
var data=$target.val();
//将特殊字符替换为“”
var dataFull=data.replace(/[^-~\s]/gi',);
//设置不带特殊字符的输入文本的新值
$target.val(数据完整);
});
});
});

尝试处理
onpaste
事件,我也尝试过,但它不起作用。到底什么不起作用?你能详细说明一下吗?与每次处理一个字符的按键不同,使用onpaste时,您必须处理已经包含无效字符的完整字符串,您只需删除它们。哦,我知道了,我声明了一个特殊字符列表,并手动检查它是否包含特殊字符。谢谢。请参考。我希望有帮助。