导航:首页 > 净水问答 > 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过滤非法字符方法相关的资料

热点内容
昌平区正规污水治理多少钱 浏览:400
超滤净水器市场单价 浏览:93
传祺cn95空调滤芯怎么样 浏览:218
污水处理厂深度处理项目 浏览:811
怎么样拆开空气净化器 浏览:829
强压提升器的工作原理 浏览:977
饮水机怎么不制冷了灯还亮 浏览:767
净水器除水垢是什么原因 浏览:537
反渗透压差含义 浏览:42
污水带锁的井盖怎么打开 浏览:507
污水用途地埋防腐管道厂家多少钱 浏览:726
戴勒森空气滤芯怎么样 浏览:497
矿井涌水水处理工艺 浏览:741
鞍山蒸馏水厂家 浏览:482
草酸除垢剂哪里有卖 浏览:719
空压过滤器350AA表示 浏览:55
洗衣机废水怎么自动排出来 浏览:708
污水厂出水余氯标准 浏览:198
宽居空气净化器怎么样 浏览:412
污水水解速率常数 浏览:482