A. js中用正則表達式 過濾特殊字元 校驗所有輸入域是否含有特殊符號
樓上2位已經說的很明白了,只允許輸入規定的字元,如果輸入含有其他字元就直接提示,不允許輸入特殊字元,或者直接給它替換掉。
B. js 如何過濾div里內的指定字元
String.replace(正則表達式,"")
replace是string類型內置的替換方法,第一個參數可以是正則表達式,第二個參數是版想要權替換成的文本,正則中可以使用/g來表示替換所有匹配的文本,不使用則代表只替換匹配到的第一個字元對象,將第二個參數設為空字元串便可達到過濾的效果。
具體正則需要你自己去了解關於正則的知識了,祝你好運。
C. 在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>
D. js字元過濾
先將表單的欄位值送入一個字元串答握變數清信慶中,如str。
然後str.indexOf("字坦腔眼")>0,屏蔽它。
E. 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
F. 如何用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;
}
G. 簡單的用js實現過濾多餘字元的正則表達式
利用正則表達法除去字元串中的重復字元
str
=
"Google"
str1
=
str.replace(/(.).*\1/g,"$1")
document.write(str
+
"
");
document.write(str1);
[Ctrl+A
全選
注:如需引入外部Js需刷新才能執行]