如何禁止在文本框中粘贴特殊字符?
我正在使用 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;
}
}
但粘贴时不屏蔽特殊字符,只在输入时屏蔽,
最佳答案
假设你有一个输入
<input id="textInput" name="textInput">
并且您有以下脚本来验证副本:
$(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);
});
});
});
关于javascript - 如何禁用特殊字符粘贴到文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16782734/