1 <!DOCTYPE html>
2 <html>
3 <head lang="en">
4 <meta charset="UTF-8">
5 <title>检验非法字符</title>
6 </head>
7 <body>
8 <input type="text" id="txt1" />
9 <button onclick="check(txt1.value)">测试文本</button>
10 <script>
11 var bForbidden = false;
12 var ch;
13 var strForbidden = new Array('<','>','&');
14 function check(str){
15 //遍历特殊字符数组和用户输入的字符串,然后比较
16 for(var i=0;i<strForbidden.length;i++){
17 for(var j=0;j<str.length;j++){
18 ch=str.substr(j,1);
19 if(ch==strForbidden[i]){//如果包含非法字符
20 bForbidden = true;
21 }
22 }
23
24 }
25 if(bForbidden){
26 alert("包含特殊字符,已经被禁止!");
27
28 }else{
29 alert("正常");
30 }
31 }
32
33
34 </script>
35
36 </body>
37 </html>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>检验非法字符</title>
</head>
<body>
<input type="text" id="txt1" />
<button onclick="check(txt1.value)">测试文本</button>
<script>
var bForbidden = false;
var ch;
var strForbidden = new Array('<','>','&');
function check(str){
//遍历特殊字符数组和用户输入的字符串,然后比较
for(var i=0;i<strForbidden.length;i++){
for(var j=0;j<str.length;j++){
ch=str.substr(j,1);
if(ch==strForbidden[i]){//如果包含非法字符
bForbidden = true;
}
}
}
if(bForbidden){
alert("包含特殊字符,已经被禁止!");
}else{
alert("正常");
}
}
</script>
</body>
</html>