如何禁用文本框中粘贴的特殊字符

JeA*_*eAr 4 html javascript jquery

如何禁用文本框中粘贴的特殊字符?

我使用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;
    }
}
Run Code Online (Sandbox Code Playgroud)

但它粘贴时不会阻止特殊字符,仅在进入时,

Cri*_* A. 11

假设你有一个输入

 <input id="textInput" name="textInput">
Run Code Online (Sandbox Code Playgroud)

并且您有以下脚本来验证副本:

$(function(){

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

    });
});
Run Code Online (Sandbox Code Playgroud)