js 非法字符检测代码

来源:百度知道 编辑:UC知道 时间:2024/06/15 07:21:17
<body>
<script language="javascript">
<!--
function contain(str,charset)// 字符串包含测试函数
{
var i;
for(i=0;i<charset.length;i++)
if(str.indexOf(charset.charAt(i))>=0)
return true;
return false;
}

function CheckForm()
{
if ((contain(document.form.NAME.value,"%\(\)><")) || (contain(document.form.MESSAGE.value,"%\(\)><")))
{
alert("输入了非法字符");
document.form.NAME.focus();
return false;
}
return true;
}
//-->
</script>

<form id="form" name="form" method="POST" action="<%=MM_editAction%>">
<label>
<input type="text" name="contain" id="contain"/>
</label>
<label>
<input type="submit

注意:浏览器会禁止运行此脚本。你必须要让浏览可以运行脚本,才行。否则浏览器仍然会提交数据。其实我建议你用Ajax来保存数据,这样就不会出现上面的问题。

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $website_title ?></title>
</head>
<body>
<script language="javascript">

function contain(str,charset)// 字符串包含测试函数
{
var i;

for(i=0;i<charset.length;i++)

if(str.indexOf(charset.charAt(i))>=0)
return true;
return fal