非法字符过滤器
⑴ 过滤多个输入框的非法字符
||function checkOtherChar(str,errmsg) {
for(var loop_index=0; loop_index<str.length; loop_index++)
{
if(str.charAt(loop_index) == '~'
||.charAt(loop_index) == '!'
||str.charAt(loop_index) == '@'
||str.charAt(loop_index) == '#'
||str.charAt(loop_index) == '$'
||str.charAt(loop_index) == '%'
||str.charAt(loop_index) == '^'
||str.charAt(loop_index) == '&'
||str.charAt(loop_index) == '*'
||str.charAt(loop_index) == '('
||str.charAt(loop_index) == ')'
||str.charAt(loop_index) == '+'
||str.charAt(loop_index) == '{'
||str.charAt(loop_index) == '}'
||str.charAt(loop_index) == '|'
||str.charAt(loop_index) == ':'
||str.charAt(loop_index) == '"'
||str.charAt(loop_index) == '<'
||str.charAt(loop_index) == '>'
||str.charAt(loop_index) == '?'
||str.charAt(loop_index) == '`'
||str.charAt(loop_index) == '='
||str.charAt(loop_index) == '['
||str.charAt(loop_index) == ']'
||str.charAt(loop_index) == '\\'
||str.charAt(loop_index) == ';'
||str.charAt(loop_index) == '\''
||str.charAt(loop_index) == ','
||str.charAt(loop_index) == '.'
||str.charAt(loop_index) == '-'
||str.charAt(loop_index) == '/')
{
//alert("~,,,!,@,#,$,%,^,&,*,+,`,\',\",:,(,),[,],{,},<,>,|,\\ and / are illegal. Please re-input.");
alert(errmsg);
return false;
}
}//end of for(loop_index)
return true;
}
⑵ 求教过滤非法字符的问题
|for (; i<sizeof(a)/sizeof(char); i++) {
if (a[i] < '0' || a[i] > '9')
; a[offset++] = a[i++]; }
改为内容
for (; i<sizeof(a)/sizeof(char); i++) {
if (a[i] < '0' || a[i] > '9')
continue ;
a[offset++] = a[i];
}
⑶ java过滤非法字符的filter
filter代码在pujia12345提供的代码上改的;
jsp页面的编码你设成你自己的,我用的是utf-8。
input.jsp输入后,正常跳转到handle.jsp,而禁词已经被过滤。
filter:
package test;
import java.io.*;
import javax.servlet.*;
import java.util.*;
public class MyFilter implements Filter
{
private List<String> unString;
public void init(FilterConfig filterConfig) throws ServletException
{
unString = new ArrayList<String>();
unString.add("日");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
String content = request.getParameter("content");//需要过滤的参数
if(content!=null){
for (int i = 0; i < unString.size(); i++)
{
String strIllegal = unString.get(i);
if (content.indexOf(strIllegal) >= 0)
{
content = content.replaceAll(strIllegal, "");//非法字符替换成空
}
request.setAttribute("content", content);//为request设置属性保存修改后的值
}
}
chain.doFilter(request, response);
}
public void destroy()
{
//System.out.println("过滤器销毁");
}
}
//---------------------------//
web.xml:
<filter>
<filter-name>myfilter</filter-name>
<filter-class>test.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
//---------------------------//
输入页面input.jsp:
<%@page contentType="text/html;charset=utf-8"%>
<!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>
<title>input.jsp</title>
</head>
<body>
<form action="handle.jsp" method="post">
<input type="text" name="content" />
<input type="submit" value=" 提交 " />
</form>
</body>
</html>
//---------------------------//
input提交的页面handle.jsp:
<%@page contentType="text/html;charset=utf-8"%>
<!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>
<title> handle.jsp </title>
</head>
<body>
<%
String content = (String)request.getAttribute("content");
out.println(content);
%>
</body>
</html>
⑷ 如何过滤掉字符串中的非法字符
过滤非法字符:
/**
* 替换xml特殊字符,
* 过滤非法字符 HJX
* @param s
* @return
*/
public static String format(String s){
String reg = "[//x00-//x08//x0b-//x0c//x0e-//x1f]";//过滤掉非法字符
if ( s == null )
return "";
else{
s=s.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">").replaceAll("/"",""").replaceAll(reg,"");;
return s;
}
}
⑸ 过滤网页上输入的非法字符,从网上找了段js代码,但是发现我用中文输入时就逃过了过滤,这个该怎么解决呢
因为中文输入的特殊字符已经失去了特殊控制意义,所以程序没有过滤中文输入的这些符号,如果你中文的符号也不放过,那么需要修改下面这个语句:
var txt=new RegExp("[ ,\\`,\\~,\\!,\\@,\#,\\$,\\%,\\^,\\+,\\*,\\&,\\\\,\\/,\\?,\\|,\\:,\\.,\\<,\\>,\\{,\\},\\(,\\),\\',\\;,\\=,\"]");
例如增加过滤@符号的的语句如下:
var txt=new RegExp("[ ,\\`,\\~,\\!,\\@,\#,\\$,\\%,\\^,\\+,\\*,\\&,\\\\,\\/,\\?,\\|,\\:,\\.,\\<,\\>,\\{,\\},\\(,\\),\\',\\;,\\=,\"@]");
添加到]前面就可以了,好像转换了中文和英文的符号,你在记事本里面修改程序代码的时候注意区别。
⑹ 怎么限制过滤非法字符注册
^咋不用正则?
var checkStrpass = document.registerUser.user.value;
re=/[^\w\-]/g;
if (re.test(checkStrpass))
{
alert("您输入的用户名包含无效回字符答!");
document.registerUser.user.focus();
return (false);
}
⑺ 求过滤SQL非法字符的函数
Function CheckSql() '防止SQL注入
Dim sql_injdata
SQL_injdata = "'||exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare"
SQL_inj = split(SQL_Injdata,"|")
If Request.QueryString<>"" Then
For Each SQL_Get In Request.QueryString
For SQL_Data=0 To Ubound(SQL_inj)
if instr(Request.QueryString(SQL_Get),Sql_Inj(Sql_DATA))>0 Then
Response.Write "<Script Language='javascript'>{alert('请不要在参数中包含非法字符!');history.back(-1)}</Script>"
Response.end
end if
next
Next
End If
If Request.Form<>"" Then
For Each Sql_Post In Request.Form
For SQL_Data=0 To Ubound(SQL_inj)
if instr(Request.Form(Sql_Post),Sql_Inj(Sql_DATA))>0 Then
Response.Write "<Script Language='javascript'>{alert('请不要在参数中包含非法字符!');history.back(-1)} </Script>"
Response.end
end if
next
next
end if
End Function
⑻ 服务器非法字符过滤
先用非法信息自查系统查一下非法字符有哪些,然后封掉这些关键字就行了!建议装个监测系统,这样问题更容易解决!
⑼ asp非法字符过滤怎么加
加我QQ 给你JS代码!
⑽ javascript 怎样过滤非法字符
你可以用过滤器来过过滤,jsp中的filter。
public class WordFilter implements Filter {
//写自己的response
class MyResponse extends HttpServletResponseWrapper{
//放字符串的
private StringWriter sw = new StringWriter();
//1.这个构造是必须是,作用是把原来的传进来进行替换
public MyResponse(HttpServletResponse arg0) {
super(arg0);
}
//2. 重写方法
@Override
public PrintWriter getWriter() throws IOException {
return new PrintWriter(sw);
}
//3.重写toString
@Override
public String toString() {
return sw.toString();
}
}
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
//替换自己的response
MyResponse response = new MyResponse((HttpServletResponse) arg1);
//让自己的response通过
arg2.doFilter(arg0, response);
//得到自己的内容
String str = response.toString();
//改一改内容
str = str.replaceAll("sb", "s*");
str = str.replaceAll("王八蛋", "??");
//传内容
response.getResponse().getOutputStream().print(str);
System.out.println("...");
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}