<script language="JavaScript">
var mikExp = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/;
function dodacheck(val)
{
var strPass = val.value;
var strLength = strPass.length;
var lchar = val.value.charAt((strLength) - 1);
if(lchar.search(mikExp) != -1)
{
var tst = val.value.substring(0, (strLength) - 1);
val.value = tst;
}
}
</script>
<input type="text" name="display1" id="display1" value="<?php echo $wc['display1']; ?>" class="width241 input fnt11" onKeyUp="javascript:dodacheck(xyz.display1);" maxlength="20">
此功能可确保不在输入框中打印特殊字符。 但是当我按下shift并按下任何特殊字符超过5秒时,它会打印出特殊字符。
如何阻止它发生?
答案 0 :(得分:1)
您应该尝试使用onKeyDown
,因为它会在用户点击密钥时触发,而不是在发布密钥时触发。
因此输入应该成为
<input type="text" name="display1" id="display1" value="<?php echo $wc['display1']; ?>" class="width241 input fnt11" onKeyDown="javascript:dodacheck(xyz.display1);" onKeyUp="javascript:dodacheck(xyz.display1);" maxlength="20">
我还添加了onKeyUp以获得额外的衡量标准:)
答案 1 :(得分:1)
试试这个,
var mikExp = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/;
function check(e) {
var keynum
var keychar
var numcheck
// For Internet Explorer
if (window.event)
{
keynum = e.keyCode
}
// For Netscape/Firefox/Opera
else if (e.which)
{
keynum = e.which
}
keychar = String.fromCharCode(keynum)
if ((keychar.toString()).search(mikExp) == 0)
{
return false;
}
else {
return true;
}
}
<input type="text" name="display1" id="display1" value="" class="width241 input fnt11" onKeypress="javascript:return check(event);" maxlength="20">