限制用户使用javascript在文本框中输入特殊字符 [英] Restrict user from entering special characters in textbox using javascript
本文介绍了限制用户使用javascript在文本框中输入特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想限制用户特定的字符,如#| %我提供了这些字符的asci代码但没有工作
我尝试过的事情:
< !DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd >
< html xmlns = http: //www.w3.org/1999/xhtml\">
< head >
< title > < / title >
< style type = text / css >
正文
{
font-size : 9pt;
font-family : Arial;
}
< / 风格 >
< / head >
< body >
字母数字值:
< 输入 type = text id = text1 / >
< span id = 错误 样式 = 颜色:红色; display:none > *不允许使用特殊字符< / span >
< script type = text / javascript >
var specialKeys = new 数组();
specialKeys.push( 8 ); // 退格
specialKeys.push( 9 ); // 选项卡
specialKeys.push( 46 跨度>); // 删除
specialKeys.push( 36 跨度>); // 主页
specialKeys.push( 35 跨度>); // 结束
specialKeys.push( 37 跨度>); // 左
specialKeys.push( 39 跨度>); // 正确
specialKeys.push( 92 跨度>); // \ |
function IsAlphaNumeric(e){
var keyCode = e.keyCode == 0 ? e.charCode:e.keyCode;
var ret =((keyCode> = 48 && keyCode< = 57 )||(keyCode> = 65 && keyCode< = 90 )||(keyCode> = 97 &&& keyCode< = 122 )||(specialKeys.indexOf(e.keyCode)!= -1&& e.charCode!= e.keyCode));
document .getElementById( error).style.display = ret? none: 内联跨度>;
返回 ret;
}
< / 脚本 >
< / body >
< / html >
解决方案
试试这个
< 正文 >
< 输入 type = text onkeypress = 返回IsAlphaNumeric(事件); < span class =code-attribute> id = text1 / >
< b id = 错误 style = display:none;颜色:红色 > 无效< / b >
< / body >
阻止输入字符的一种非常标准的方法是函数event.preventDefault
:
https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault [ ^ ]。
另见我过去的答案:如何删除按键事件中键入的字符 [ ^ ]。
这个简短的手册也很有用:键盘文本输入过滤:示例 [ ^ ]。
一定要允许退格,这也被认为是代号为8的角色。
-SA
i want to restrict user specific characters like # | % i have supplied asci code of those characters but not working
What I have tried:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
body
{
font-size: 9pt;
font-family: Arial;
}
</style>
</head>
<body>
Alphanumeric value:
<input type="text" id="text1" />
<span id="error" style="color: Red; display: none">* Special Characters not allowed</span>
<script type="text/javascript">
var specialKeys = new Array();
specialKeys.push(8); //Backspace
specialKeys.push(9); //Tab
specialKeys.push(46); //Delete
specialKeys.push(36); //Home
specialKeys.push(35); //End
specialKeys.push(37); //Left
specialKeys.push(39); //Right
specialKeys.push(92); //\|
function IsAlphaNumeric(e) {
var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode));
document.getElementById("error").style.display = ret ? "none" : "inline";
return ret;
}
</script>
</body>
</html>
解决方案
try this
<body> <input type="text" onkeypress="return IsAlphaNumeric(event);" id="text1" /> <b id="error" style="display:none; color:red">Invalid</b> </body>
A really standard way to block input of a character is the functionevent.preventDefault
:
https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault[^].
See also my past answer: how to delete the character typed in key press event[^].
This short manual can also be useful: Keyboard Text Input Filtering: Examples[^].
Be sure to allow backspace, which is also considered as a character with the code point 8.
—SA
这篇关于限制用户使用javascript在文本框中输入特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文