禁用特殊字符的输入

reciment 2013-04-27 03:26:48

function showKeyPress(evt) {
evt = (evt) ? evt : window.event
return checkSpecificKey(evt.keyCode);
}
///
function checkSpecificKey(keyCode) {
var specialKey = "$%\:\;\~\,\,\//\\\\^*\'\"\+\|\?"; //Specific Key list
var realkey = String.fromCharCode(keyCode);
var flg = false;
flg = (specialKey.indexOf(realkey) >= 0);
if (flg) {
//alert('请勿输入特殊字符: ' + realkey);
return false;
}
return true;
}
document.onkeypress = showKeyPress;

///同一个页面中用户名需要对特殊字符进行控制,密码不需要,但是页面中写入以上js
///页面整体验证不通过。
<input type="text" name="userName" onkeypress =showKeyPress()/>
<input type="text" name="userpassword"/>




...全文
377 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
cfd406635982 2013-06-20
  • 打赏
  • 举报
回复
 &glt;<>
cfd406635982 2013-06-20
  • 打赏
  • 举报
回复
<scriptt>alert('ok');</script>
reciment 2013-04-27
  • 打赏
  • 举报
回复
thank you
functionsub 2013-04-27
  • 打赏
  • 举报
回复
还有就是
var realkey = String.fromCharCode(keyCode);
这里取出来的值是不对的,还是用正则匹配吧。
<script type="text/javascript">
function showKeyPress(evt,obj) {
    evt = (evt) ? evt : window.event
    return checkSpecificKey(evt.keyCode,obj);
}
///
function checkSpecificKey(keyCode,obj) {
    var reg = /[\$\%\\\:\\\;\~\,\,\//\\\\^*\'\"\+\|\?]/g; //Specific Key list
    var val = obj.value;
    var flg = reg.test(val);
    if(flg){
        val = val.replace(reg,'')
        console.log(val)
        obj.value = val;
    }
}

 </script>
///同一个页面中用户名需要对特殊字符进行控制,密码不需要,但是页面中写入以上js
///页面整体验证不通过。
<input type="text"  name="userName" onkeyup ="return showKeyPress(event,this)"/>
<input type="text"  name="userpassword"/>
reciment 2013-04-27
  • 打赏
  • 举报
回复
貌似只有onkeypress才有效 而且禁用掉 document.onkeypress = showKeyPress;貌似操作无效。
reciment 2013-04-27
  • 打赏
  • 举报
回复
还是无效 可以直接输入特殊字符

 onkeydown="javascript:return showKeyPress(event)"  无效
functionsub 2013-04-27
  • 打赏
  • 举报
回复
document的事件去掉。 onkeypress换成onkeydown或者onkeyup onkeypress的时候在FF下表现的很奇怪,有值输入进去的时候keycode一直是0(回车什么的不是0),charcode倒是有值。
reciment 2013-04-27
  • 打赏
  • 举报
回复
document.onkeypress = showKeyPress;
reciment 2013-04-27
  • 打赏
  • 举报
回复
引用 3 楼 functionsub 的回复:
<input type="text"  name="userName" onkeypress ="return showKeyPress(event)" />
还是不行。。。。。 是不是因为

document.onkeypress = showKeyPress;
functionsub 2013-04-27
  • 打赏
  • 举报
回复
<input type="text"  name="userName" onkeypress ="return showKeyPress(event)" />
reciment 2013-04-27
  • 打赏
  • 举报
回复
貌似不行哦。。。。。
functionsub 2013-04-27
  • 打赏
  • 举报
回复
<input type="text"  name="userName" onkeypress ="return showKeyPress()" />

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧