01 var $j = jQuery.noConflict();
02 //使用此方法验证所有text textarea 是否含有非法字符
03 $j(document).ready(function(){
04 $j("input[type='text']").each(function(i){
05 $j(this).keyup(function() { checkIllegalChar($j(this)); });
06 });
07 $j("textarea").each(function(i){
08 $j(this).keyup(function() { checkIllegalChar($j(this)); });
09 });
10 });
11
12
13
14 function checkIllegalChar(obj){
15 var value = obj.val();
16 if(!checkChar(value)){
17 obj.val(value.substring(0,value.length-1));
18 obj.focus();
19
20 }
21 }
22
23 //检查输入中的非法字符
24 function checkChar(InString) {
25 var RefString = "<";
26 var RefString2 = "%";
27 var RefString3 = "\"";
28 var RefString4 = ">";
29 var RefString5 = "~";
30 var RefString6 = "&";
31 var RefString7 = "?";
32 var RefString8 = "'";
33 for (Count = 0; Count < InString.length; Count++) {
34 TempChar = InString.substring(Count, Count + 1);
35 if ((RefString.indexOf(TempChar, 0) == 0) || (RefString2.indexOf(TempChar, 0) == 0) || (RefString3.indexOf(TempChar, 0) == 0) || (RefString4.indexOf(TempChar, 0) == 0) || (RefString5.indexOf(TempChar, 0) == 0) || (RefString6.indexOf(TempChar, 0) == 0) || (RefString7.indexOf(TempChar, 0) ==0) || (RefString8.indexOf(TempChar, 0) == 0)) {
36 alert("您的输入中含有非法字符\"<\",\"\"\",\"%\",\"> \",\"~\",\"&\",\"?\",\"'\",请重新输入!");
37 return (false);
38 }
39 }
40 return (true);
41 }