Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在提交按钮上阻止特殊字符验证_Javascript_Jsp - Fatal编程技术网

Javascript 在提交按钮上阻止特殊字符验证

Javascript 在提交按钮上阻止特殊字符验证,javascript,jsp,Javascript,Jsp,我试着用这段代码来允许在文本框中复制和粘贴,并在提交按钮上验证它。但它不起作用。任何人想提出建议都将不胜感激 $('#businessId').bind('keypress', function(e) { blockSpecialCharacter(e); }); var regex = new RegExp("^[a-zA-Z0-9]+$"); function blockSpecialCharacter(event) { var key = Str

我试着用这段代码来允许在文本框中复制和粘贴,并在提交按钮上验证它。但它不起作用。任何人想提出建议都将不胜感激

$('#businessId').bind('keypress', function(e) {
     blockSpecialCharacter(e);
   });

   var regex = new RegExp("^[a-zA-Z0-9]+$");
    function blockSpecialCharacter(event) {
      var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
      if (!regex.test(key)) {
         event.preventDefault();
         return false;
      }
    }




function blockPaste(event) {
  var pastedData = event.originalEvent.clipboardData.getData('text/plain');
  if (!regex.test(pastedData)) {
     event.preventDefault();
     return false;
  }
}

<div class="buttons">
      <input type="submit" onclick="return blockPaste(e);" value="<spring:message code="button.save" />" />
      <input type="submit" value="<spring:message code="button.cancel" />" name="_cancel"/>
</div>
$('businessId').bind('keypress',函数(e){
区块特殊字符(e);
});
var regex=new RegExp(“^[a-zA-Z0-9]+$”;
功能块特殊字符(事件){
var key=String.fromCharCode(!event.charCode?event.which:event.charCode);
如果(!正则表达式测试(键)){
event.preventDefault();
返回false;
}
}
功能块粘贴(事件){
var pastedData=event.originalEvent.clipboardData.getData('text/plain');
如果(!正则表达式测试(粘贴数据)){
event.preventDefault();
返回false;
}
}

最好使用更改事件而不是按键。因为更改也将在复制粘贴时激活。不,不工作。您需要共享整个代码。