javascript 不允许粘贴任何非字母数字字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3311927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 00:49:12  来源:igfitidea点击:

Do not allow Paste any non alphanumeric characters

javascriptvalidation

提问by user228777

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!!

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

采纳答案by Deepak

You can use the onblurevent of text box.

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

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.

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

回答by chryss

Using jQuery, this is one way to do it:

使用 jQuery,这是一种方法:

HTML:

HTML:

?<form name='theform' id='theform' action=''>
<textarea id='nonumbers' cols='60' rows='10'> </textarea>
</form>????????????????????????????????????????????????????????????????????????????

JavaScript:

JavaScript:

$().ready(function(){
    $("textarea#nonumbers").keyup(removeextra).blur(removeextra);
});
function removeextra() {
    var initVal = $(this).val();
    outputVal = initVal.replace(/[^0-9a-zA-Z]/g,"");       
    if (initVal != outputVal) {
        $(this).val(outputVal);
    }
};

Try it out here.

在这里尝试一下。

EDIT: As remarked in the comments, the original (using the .keyup() event) would have left open the possibility of pasting via the mouse context menu, so I've added a .blur() event. .change() would have been possible too, but there are reports of bugginess. Another option is using .focusout(). Time to experiment...

编辑:正如评论中所述,原始(使用 .keyup() 事件)会保留通过鼠标上下文菜单粘贴的可能性,所以我添加了一个 .blur() 事件。.change() 也是可能的,但有报告称存在错误。另一种选择是使用 .focusout()。实验时间...

回答by sohtimsso1970

Why has no one suggested using the OnPasteevent? That is fully supported in IE, Safari and Chrome.

为什么没有人建议使用该OnPaste事件?这在 IE、Safari 和 Chrome 中完全支持。

Docs for using OnPaste in IE

在 IE 中使用 OnPaste 的文档

Docs for using OnPaste in Webkit

在 Webkit 中使用 OnPaste 的文档

In JQuery, that would look like this:

在 JQuery 中,它看起来像这样:

$(input).bind("paste", function(e){ RemoveNonAlphaNumeric(); })

That covers 75% of the browser market.

这涵盖了 75% 的浏览器市场。

If you use JQuery, OnPaste is automatically normalized in Firefox so that it works there too. If you can't use JQuery, there is an OnInputevent that works.

如果您使用 JQuery,OnPaste 会在 Firefox 中自动规范化,以便在那里也能正常工作。如果您不能使用 JQuery,则有一个有效的OnInput事件。

The working solution is to use a fast setTimeout value to allow the input's value property to be filled.

有效的解决方案是使用快速 setTimeout 值来填充输入的 value 属性。

Basically like this:

基本上是这样的:

$("input").bind("paste", function(e){RemoveAlphaChars(this, e);});

function RemoveAlphaChars(txt, e)
{
    setTimeout(function()
    {
        var initVal = $(txt).val();

        outputVal = initVal.replace(/[^0-9]/g,"");

        if (initVal != outputVal)
            $(txt).val(outputVal);
    },1);
}

I have tested this in IE, Chrome and Firefox and it works well. The timeout is so fast, you can't even see the characters that are being removed.

我已经在 IE、Chrome 和 Firefox 中对此进行了测试,并且效果很好。超时如此之快,您甚至看不到正在删除的字符。

回答by Peter Loron

I'm not sure how you can prevent pasting, but you can filter the contents either on the submit or on the change event.

我不确定如何防止粘贴,但您可以在提交或更改事件中过滤内容。

回答by dmitko

It's better to validate form - check this great jQuery's plugin - http://docs.jquery.com/Plugins/Validation/and you can use the "number" rule : http://docs.jquery.com/Plugins/Validation/Methods/number. Really simple and easy to tune thing!

最好验证表单 - 检查这个很棒的 jQuery 插件 - http://docs.jquery.com/Plugins/Validation/你可以使用“数字”规则:http: //docs.jquery.com/Plugins/Validation/方法/数量。真的很简单,容易调整的东西!

回答by Weston C

Assuming:

假设:

<textarea id="t1"/>

You could modify the onchangeevent handler for the textarea to strip out anything not alphanumeric:

您可以修改onchangetextarea的事件处理程序以去除任何非字母数字的内容:

document.getElementById('t1').onchange = function () {
    this.value = this.value.replace(/\W/,'');
}