㈠ js中用正則表達式 過濾特殊字元 校驗所有輸入域是否含有特殊符號
function stripscript(s) {
var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#¥……&*()——|{}【】『;:」「'。,、?]")
var rs = "";
for (var i = 0; i < s.length; i++) {
rs = rs + s.substr(i, 1).replace(pattern, '');
}
return rs;
}
㈡ 求一段JS過濾臟話的代碼
可以研究一下正則表達式:
var s="你好哎呦,嘟嘟,我們一起去吃飯吧?";//這個假設是你表單的文字
var reg=/(哎呦)|(嘟嘟)/g;//這個就是正則式了,將想過濾的詞彙放在這里
var str=s.match(reg).join("\",\"");//match可以將符合的詞彙挑出來組成一個數組
alert("請不要使用\""+str+"\"等不文明詞彙!");
㈢ 用JS如何寫正則表達式 用了屏蔽用戶輸入的非法文字
var valid=/[!@#$%^&*()]/; //你認為哪個是非常字元就加進去
var input;
....
if(!valid.test(input)){
//錯誤信息
}
㈣ 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;
}