導航:首頁 > 凈水問答 > javascript過濾非法字元方法

javascript過濾非法字元方法

發布時間:2021-02-01 21:57:32

『壹』 js用什麼函數過濾非法字元防止跨站腳本攻擊

test
如:抄<input type="text" id="txtceshi" /><input type="button" onclick="yanzheng()" value="ceshi" />
<script type="text/javascript">
function yanzheng() {
var ce = txtceshi.value;//獲取文本框的值
var ze = /[^0-9]+/; //只能是數字,這里可以在網上找到一些正則替換成你想要的表達式
if (ze.test(ce)) {
alert("數據不合法!");
}
}
</script>

『貳』 如何過濾掉字元串中的非法字元

過濾來非法字元:自
/**
* 替換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("[ ,\\`,\\~,\\!,\\@,\#,\\$,\\%,\\^,\\+,\\*,\\&,\\\\,\\/,\\?,\\|,\\:,\\.,\\<,\\>,\\{,\\},\\(,\\),\\',\\;,\\=,\"@]");

添加到]前面就可以了,好像轉換了中文和英文的符號,你在記事本裡面修改程序代碼的時候注意區別。

『肆』 在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>

『伍』 js 正則過濾特殊字元

您好

js檢查是否含有非法字元,js 正則過濾特殊字元

//正則
functiontrimTxt(txt){
returntxt.replace(/(^s*)|(s*$)/g,"");
}

/**
*檢查是否含有非法字元
*@paramtemp_str
*@returns{Boolean}
*/
functionis_forbid(temp_str){
temp_str=trimTxt(temp_str);
temp_str=temp_str.replace('*',"@");
temp_str=temp_str.replace('--',"@");
temp_str=temp_str.replace('/',"@");
temp_str=temp_str.replace('+',"@");
temp_str=temp_str.replace(''',"@");
temp_str=temp_str.replace('\',"@");
temp_str=temp_str.replace('$',"@");
temp_str=temp_str.replace('^',"@");
temp_str=temp_str.replace('.',"@");
temp_str=temp_str.replace(';',"@");
temp_str=temp_str.replace('<',"@");
temp_str=temp_str.replace('>',"@");
temp_str=temp_str.replace('"',"@");
temp_str=temp_str.replace('=',"@");
temp_str=temp_str.replace('{',"@");
temp_str=temp_str.replace('}',"@");
varforbid_str=newString('@,%,~,&');
varforbid_array=newArray();
forbid_array=forbid_str.split(',');
for(i=0;i<forbid_array.length;i++){
if(temp_str.search(newRegExp(forbid_array[i]))!=-1)
returnfalse;
}
returntrue;
}

---------------------

作者:dongsir 董先生

來源:董先生的博客

原文鏈接:js檢查是否含有非法字元

版權聲明:本作品採用知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協議進行許可。轉載時請標註:http://dongsir.cn/p/195

『陸』 如何用js或則jquery過濾特殊字元

1、jQuery使用正則匹配替換特殊字元

functionRegeMatch(){
varpattern=newRegExp("[~'!@#$%^&*()-+_=:]");
if($("#name").val()!=""&&$("#name").val()!=null){
if(pattern.test($("#name").val())){
alert("非法字元!");
$("#name").attr("value","");
$("#name").focus();
returnfalse;
}
}
}

2、jQuery限制輸入ASCII值

//數字0-9的ascii為48-57
//大寫A-Z的ascii為65-90
//小寫a-z的ascii為97-122

//----------------------------------------------------------------------
//<summary>
//限制只能輸入數字和字母
//</summary>
//----------------------------------------------------------------------
$.fn.onlyNumAlpha=function(){
$(this).keypress(function(event){
vareventObj=event||e;
varkeyCode=eventObj.keyCode||eventObj.which;
if((keyCode>=48&&keyCode<=57)||(keyCode>=65&&keyCode<=90)||(keyCode>=97&&keyCode<=122))
returntrue;
else
returnfalse;
}).focus(function(){
this.style.imeMode='disabled';
}).bind("paste",function(){
varclipboard=window.clipboardData.getData("Text");
if(/^(d|[a-zA-Z])+$/.test(clipboard))
returntrue;
else
returnfalse;
});
};


//-----調用方法$("#文本框id").onlyNumAlpha();


3、js正則匹配過濾

functionstripscript(s)
{
varpattern=newRegExp("[`~!@#$^&*()=|{}':;',\[\].<>/?~!@#¥……&*()——|{}【】『;:」「'。,、?]")
varrs="";
for(vari=0;i<s.length;i++){
rs=rs+s.substr(i,1).replace(pattern,'');
}
returnrs;
}

『柒』 幾種過濾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檢測非法字元代碼

注意:瀏覽器會禁止運行此腳本。你必須要讓瀏覽可以運行腳本,才行。否則瀏覽器仍然會提交數據。其實我建議你用Ajax來保存數據,這樣就不會出現上面的問題。

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $website_title ?></title>
</head>
<body>
<script language="javascript">

function contain(str,charset)// 字元串包含測試函數
{
var i;

for(i=0;i<charset.length;i++)

if(str.indexOf(charset.charAt(i))>=0)
return true;
return false;
}

function CheckForm()
{

var oContain = document.getElementById("contain");

if(contain(oContain.value,"%\(\)><")){
alert("輸入了非法字元");
return false;
}else{
return true;
// alert("true");
}
// if ((contain(document.form.NAME.value,"%\(\)><")) || (contain(document.form.MESSAGE.value,"%\(\)><")))
// {
//
// alert("輸入了非法字元");
// document.form.NAME.focus();
// return false;
// }else{
// alert("dddd");
// return true;
// }

}

</script>

<form id="form" name="form" onsubmit='if(CheckForm())return true;else return false;' method="POST" action="<%=MM_editAction%>">
<label>
<input type="text" id="contain" name="contain" id="contain"/>
</label>
<label>
<input type="submit" name="Submit" value="提交" />
</label>
<input type="hidden" name="MM_insert" value="form">
</form>
</body>

『玖』 javascript 怎樣過濾非法字元

你可以用過濾器來過過濾,中的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

}

}

『拾』 javascript里不能含有非法字元的正則表達式如何寫

^^/[@#\$%\^&\*]+/g 這個是包含來以上任意一個特殊字自符的。
你可以測試一下:
alert(/[@#\$%\^&\*]+/g.test("你的字元串"))
如果返回是true,那麼說明含有非法字元,用!反一下,就行了,如:
alert(!/[@#\$%\^&\*]+/g.test("你的字元串"))

閱讀全文

與javascript過濾非法字元方法相關的資料

熱點內容
地熱過濾網漏水 瀏覽:705
簡述活性污泥法凈化污水的機理 瀏覽:807
污水口設置屬什麼部門監管 瀏覽:826
華帝凈化器前蓋怎麼打開 瀏覽:530
tbpb對不飽和聚酯樹脂的影響 瀏覽:715
污水進水總磷高是什麼原因 瀏覽:575
家裡地暖不熱過濾閥 瀏覽:880
含氰廢水降低ph 瀏覽:157
小米凈化器pro數據怎麼看 瀏覽:139
廢水的ss值 瀏覽:693
小區室外雨污水管道圖紙怎麼看 瀏覽:554
污水井剖面圖尺寸 瀏覽:588
安利凈水器濾芯外面的膜是什麼 瀏覽:308
酒店的凈水器是怎麼用的 瀏覽:338
除污水為什麼要預氧化 瀏覽:674
小米車載凈化器怎麼走線 瀏覽:296
為什麼打開回水閥排出來污水 瀏覽:412
純凈水桶子有刻劃怎麼辦 瀏覽:827
蒸餾餅的做法 瀏覽:171
蒸餾水存在丁達爾效應嗎 瀏覽:10