非法字元過濾器
⑴ 過濾多個輸入框的非法字元
||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
}
}