如何禁用特殊字符粘贴到文本框中 [英] How to disable special characters from paste in a textbox
本文介绍了如何禁用特殊字符粘贴到文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何禁止在文本框中粘贴特殊字符?
How to disable special characters from paste in a textbox?
我正在使用 onkeypress 事件处理程序
Im using a onkeypress event handler
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;
}
}
但它在粘贴时不会屏蔽特殊字符,仅在输入时,
But it doesnt block special characters when pasting, only in entering,
推荐答案
假设你有一个 Input
suppose that you have a Input
<input id="textInput" name="textInput">
并且您有以下脚本来验证副本:
and you have the following script to validate the copy:
$(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);
});
});
});
这篇关于如何禁用特殊字符粘贴到文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文