Ⅰ jsp中如何过滤非法字符
可以利用javascript验证正则表达式的方式对非法字符做处理,同样Java也可以利用正则表达式帮你过滤非法字符,最终还得更具你的具体需求来定。
Ⅱ 在javascript中用正则表达式过滤指定的字符(一定要能指定!)
楼上的不加转义字符\ 你们搞什么啊
正确的应该是这样的
加入你得到的内字符窜容为 name
<html>
<head>
<script>
function test1(){
var name=document.getElementById('user').value;
name=name.replace(/(\!+)|(\<+)|(\>+)|(\'+)/g,"");
alert(name);
}
</script>
</head>
<body>
<input type="text" id="user" />
<input type="button" value="te" onclick="test1()">
</body>
</html>
Ⅲ 几种过滤URL和FORM中非法字符的方法
ASP过滤URL和FORM中非法字符第一种:<%'检查URL输入限制非法字符url=LCase(request.querystring())ip=request.ServerVariables(
REMOTE_ADDR)pos1=instr(url,%)pos2=instr(url,')pos3=instr(url,;)pos4=instr(url,where)pos5=instr(url,select)pos6=instr(url,chr)pos7=instr(url,/)pos8=Instr(url,and)ifpos1<0orpos2<0orpos3<0orpos4<0orpos5<0orpos6<0orpos7<0orpos8<0thenresponse.Write
你尝试使用危险字符,系统已经对此做了记录如下您的IP:&ip&操作时间:&date()&
response.End()endif'检查表单输入,限制非法字符'使用request.QueryString来索引request的所有资料,作为SQL检查之用'如出现非法字符则自动停止输出fori_request=1torequest.form.Countifinstr(request.form(i_request),')<0orinstr(request.form(i_request),;)<0thenResponse.Write
<scriptlanguage='javascript'history.back();alert('你尝试使用危险字符,系统已经对此做了记录如下您的IP:&ip&操作时间:&date()&
');</script
response.End()endifnext%第二种:<%OnErrorResumeNextdimsql_injdata,sql_inj,sql_get,sql_data
SQL_injdata='|oxSQL_inj=split(SQL_Injdata,|)'定义过滤字符,可以自己添加,以|分隔''|;|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare'对post方式过滤IfRequest.Form<ThenForEachSql_PostInRequest.FormForSQL_Data=0ToUbound(SQL_inj)ifinstr(Request.Form(Sql_Post),Sql_Inj(Sql_DATA))0ThenResponse.redirectss'出错时转向页面
Response.endendifnextnextendif'对GET方式过滤IfRequest.QueryString<ThenForEachSQL_GetInRequest.QueryStringForSQL_Data=0ToUbound(SQL_inj)ifinstr(Request.QueryString(SQL_Get),Sql_Inj(Sql_DATA))0ThenResponse.redirectss'出错时转向页面
Response.endendifnextNextEndIf%第三种:functioncheckstr(str)'过滤非法字符函数dimtempstrifstr=thenexitfunctiontempstr=replace(str,
chr(34),)'tempstr=replace(tempstr,
chr(39),)''tempstr=replace(tempstr,
chr(60),)'<tempstr=replace(tempstr,
chr(62),)'tempstr=replace(tempstr,
chr(37),)'%tempstr=replace(tempstr,
chr(38),)'&tempstr=replace(tempstr,
chr(40),)'(tempstr=replace(tempstr,
chr(41),)')tempstr=replace(tempstr,
chr(59),)';tempstr=replace(tempstr,
chr(43),)'+tempstr=replace(tempstr,
chr(45),)'-tempstr=replace(tempstr,
chr(91),)'[tempstr=replace(tempstr,
chr(93),)']tempstr=replace(tempstr,
chr(123),)'{tempstr=replace(tempstr,
chr(125),)'}checkstr=tempstrendfunction第四种:'================================================'函数名:IsValidStr'作用:判断字符串中是否含有非法字符'参数:str----原字符串'返回值:False‚True-----布尔值'================================================PublicFunctionIsValidStr(ByValstr)IsValidStr=(str)ThenExitFunctionIfTrim(str)=‚iForbidStr=
and|chr|:|=|%|&|$|#|@|+|-|*|/|/|<||;|‚|^|&Chr(32)&|&Chr(34)&|&Chr(39)&|&Chr(9)ForbidStr=Split(ForbidStr‚|)Fori=0ToUBound(ForbidStr)IfInStr(1‚str‚ForbidStr(i)‚1)0ThenIsValidStr==TrueEndFunctionASP.(Stringpara)//过滤非法字符{intflag=0;flag+=para.indexOf(')+1;flag+=para.indexOf(;)+1;flag+=para.indexOf(1=1)+1;flag+=para.indexOf(|)+1;flag+=para.indexOf(<)+1;flag+=para.indexOf()+1;if(flag!=0){System.
out
提交了非法字符!!!);returnfalse;}returntrue;}
Ⅳ 过滤网页上输入的非法字符,从网上找了段js代码,但是发现我用中文输入时就逃过了过滤,这个该怎么解决呢
因为中文输入的特殊字符已经失去了特殊控制意义,所以程序没有过滤中文输入的这些符号,如果你中文的符号也不放过,那么需要修改下面这个语句:
var txt=new RegExp("[ ,\\`,\\~,\\!,\\@,\#,\\$,\\%,\\^,\\+,\\*,\\&,\\\\,\\/,\\?,\\|,\\:,\\.,\\<,\\>,\\{,\\},\\(,\\),\\',\\;,\\=,\"]");
例如增加过滤@符号的的语句如下:
var txt=new RegExp("[ ,\\`,\\~,\\!,\\@,\#,\\$,\\%,\\^,\\+,\\*,\\&,\\\\,\\/,\\?,\\|,\\:,\\.,\\<,\\>,\\{,\\},\\(,\\),\\',\\;,\\=,\"@]");
添加到]前面就可以了,好像转换了中文和英文的符号,你在记事本里面修改程序代码的时候注意区别。