當前位置:首頁 » 凈水方式 » js過濾非法字元

js過濾非法字元

發布時間: 2021-03-30 06:31:09

過濾多個輸入框的非法字元

||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;
}

㈡ 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 怎樣過濾非法字元

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

}

}

㈣ 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>

㈤ 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

㈥ 如何過濾掉字元串中的非法字元

過濾非法字元:
/**
* 替換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非法字元檢測

1、用RegExp的exec()方法,該方法每次返回一個匹配,找不到時返回null

2、例如,str='1111哈哈嘿嘿',第一次re.exec(str)返回'哈哈',第二次re.exec(str)返回'嘿嘿',第三次返回null。如下圖:

㈧ js 替換文本框中非法字元

把alert去掉不就行了嗎?
function checkChar(){
var inputobj=document.getElementById("uname1"); //得到文本框對象
inputobj.value=inputobj.value.TextFilter(); //用TextFilter()過濾文本框的值
}

//TextFilter()函數直接把限定字元替換為空了,如果替換為空格的話修改該函數即可,
//在for循環中把 rs+=this.substr(i,1).replace(pattern,'');
//改成 rs+=this.substr(i,1).replace(pattern,' ');

ps:這種簡單的過濾用正則了還要用for循環,實在不欣賞這樣的寫法

一個函數就搞定了,擴展String對象實在沒必要:
function checkChar(){
var inputobj=document.getElementById("uname1"); //得到文本框對象
inputobj.value=inputobj.value.replace(/[<>]/g," "); //直接正則替換就OK了
}

㈨ 如何用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;
}

㈩ 過濾網頁上輸入的非法字元,從網上找了段js代碼,但是發現我用中文輸入時就逃過了過濾,這個該怎麼解決呢

因為中文輸入的特殊字元已經失去了特殊控制意義,所以程序沒有過濾中文輸入的這些符號,如果你中文的符號也不放過,那麼需要修改下面這個語句:
var txt=new RegExp("[ ,\\`,\\~,\\!,\\@,\#,\\$,\\%,\\^,\\+,\\*,\\&,\\\\,\\/,\\?,\\|,\\:,\\.,\\<,\\>,\\{,\\},\\(,\\),\\',\\;,\\=,\"]");

例如增加過濾@符號的的語句如下:
var txt=new RegExp("[ ,\\`,\\~,\\!,\\@,\#,\\$,\\%,\\^,\\+,\\*,\\&,\\\\,\\/,\\?,\\|,\\:,\\.,\\<,\\>,\\{,\\},\\(,\\),\\',\\;,\\=,\"@]");

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

熱點內容
丁度巴拉斯情人電影推薦 發布:2024-08-19 09:13:07 瀏覽:886
類似深水的露點電影 發布:2024-08-19 09:10:12 瀏覽:80
《消失的眼角膜》2電影 發布:2024-08-19 08:34:43 瀏覽:878
私人影院什麼電影好看 發布:2024-08-19 08:33:32 瀏覽:593
干 B 發布:2024-08-19 08:30:21 瀏覽:910
夜晚看片網站 發布:2024-08-19 08:20:59 瀏覽:440
台灣男同電影《越界》 發布:2024-08-19 08:04:35 瀏覽:290
看電影選座位追女孩 發布:2024-08-19 07:54:42 瀏覽:975
日本a級愛情 發布:2024-08-19 07:30:38 瀏覽:832
生活中的瑪麗類似電影 發布:2024-08-19 07:26:46 瀏覽:239