检查输入/文本区域中粘贴的文本是否存在无效的章程

Check pasted text in input/textarea for non valid charters

本文关键字:文本 是否 存在 无效 输入 区域 检查      更新时间:2023-09-26

我有输入字段,我需要检查无效的章程。我使用 jQuery .on('keyup keypress')阻止所有无效符号的函数,但是当有人使用 CTRL+V 时 - 所有符号都被粘贴:(

1234567890zxcvbnmasdfghjklqwertyuiopABCDEFGHIJKLMNOPQRSTUVWXYZ"''- №&

允许使用带有包机的字符串。如何与.onclick()/bind('paste')核实是否不允许某些包机。

感谢。

您可以使用

/[A-Za-z0-9'"''''-№&/g匹配任何不需要的字符,将它们替换为空字符,并检查字符串是否为空。

注意:我不确定是否有字符串的包含方法,我需要检查。

建议对

粘贴上的内容进行消毒,例如:

<input type="text" id="the-input" onkeydown="return yourFunctioToGetKeyDown(event)">

还有一个消毒事件:

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

查看示例 https://jsfiddle.net/MarcelKohls/Lg3a9opo/