js过滤特殊符号
Ⅰ js、jQuery如何过滤特殊字符(* 和/)
keyword=keyword.replace(/[\*\/]/g,"")
Ⅱ 如何用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中用正则表达式 过滤特殊字符 校验所有输入域是否含有特殊符号
楼上2位已经说的很明白了,只允许输入规定的字符,如果输入含有其他字符就直接提示,不允许输入特殊字符,或者直接给它替换掉。
Ⅳ 谁有JS过滤特殊字符的代码,发我一份,特殊字符类似于(!@#¥%……&*)等等,就是类似这种,
functionstripscript(s)
{
varpattern=newRegExp("[`~!@来#$^&*()=|{}':;',\[\].<>/?~!@源#¥……&*()——|{}【】‘;:”“'。,、?]")
varrs="";
for(vari=0;i<s.length;i++){
rs=rs+s.substr(i,1).replace(pattern,'');
}
returnrs;
}
用正则过滤一下
Ⅳ js过滤json数据特殊字符
用replace函数替换
例如替换换行为空格
text.replace(/\n+/,' ')
Ⅵ JS如何去除指定字符串
可以用replace函数去除指定字符串。
1、在body标签和html标签中添加一个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
Ⅷ 求js去除字符串中所有 和&等特殊符号。
vara="今天是星期五,明天又可以放假了&好好休|息一下"
varb=a.replace(/[&|\*^%$#@-]/g,"");
alert(b);
需要去掉什么符号,就在正则表达式中加上什么符号
Ⅸ 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正则表达式去除特殊字符
正则表达式只能去测试字符串适合符合,返回true,false。并不能改变你的字符串(也就是说去除功能)