当前位置: 首页 > 图文教程 > 网络编程 > Javascript > JS 控制非法字符的输入代码

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 JS 控制非法字符的输入代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 395 ::
收藏到网摘: n/a

JS控制非法字符的输入实现代码,需要的朋友可以参考下。 html文件代码如下:
复制代码 代码如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JS控制非法字符的输入</title>
</head>
<body>
<form>
<p>这里不允许输入如下字符:(像!@#$%^&*等)<br>
<textarea rows="2" cols="20" name="comments" onkeypress="checkComments()"></textarea>
</p>
<p>这里不允许输入引号:<br>
<input type="text" name="txtEmail" onkeypress="checkEmail()"/>
</p>
<p>这里只能输入数字:<br>
<input type="text" name="txtPostalCode" onkeypress="checkPostalCode()"/>
</p>
<p>这里只能输入大写英文:<br>
<input type="text" name="txtEnglish" onkeypress="checkEnglish()"/>
</p>
</form>
</body>
</html>

js文件代码如下:
复制代码 代码如下:

<script type="text/javascript" language="JavaScript">
/*
* 特殊字符在ASCII码中所表示的范围为32~48,57~65,90~97
* event.returnValue=false;设置键盘输入主false,则不能在文本框中输入内容
*/
function checkComments(){
if (( event.keyCode > 32 && event.keyCode < 48) ||
( event.keyCode > 57 && event.keyCode < 65) ||
( event.keyCode > 90 && event.keyCode < 97)
) {
event.returnValue = false;
}
}
/*
* 引号的ASCII码为34和39
*/
function checkEmail(){
if ( event.keyCode == 34 || event.keyCode == 39 ) {
event.returnValue = false;
}
}
/*
* 数字的ASCII表示范围为 45~57
*/
function checkPostalCode() {
if( event.keyCode < 45 || event.keyCode >57 ) {
event.returnValue = false;
}
}
/*
* 大写英文字母ASCII表示范围为65~91
* 小写英文字母ASCII表示范围为97~123
*/
function checkEnglish() {
if( event.keyCode < 65 || event.keyCode > 91 ) {
event.returnValue = false;
}
}
</script>