<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script language ="javascript">
function ValidateValue(textbox)
{
var IllegalString = "\`~@#;,.!#$%^&*()+{}|\\:\"<>?-=/,\'";
var textboxvalue = textbox.value;
var index = textboxvalue.length - 1;
var s = textbox.value.charAt(index);
if(IllegalString.indexOf(s)>=0)
{
s = textboxvalue.substring(0,index);
textbox.value = s;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Text1" runat="server" type="text" onkeyup = "ValidateValue(this)"/></div>
</form>
</body>
</html>
本文介绍了一种使用JavaScript在HTML页面中实现对输入框内非法字符进行过滤的方法。通过定义一系列非法字符并利用字符串操作函数,确保用户无法输入这些可能引起安全问题或格式错误的字符。
3645

被折叠的 条评论
为什么被折叠?



