导航:首页 > 编程语言 > js过滤输入字符

js过滤输入字符

发布时间:2023-07-03 06:14:03

『壹』 在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中用正则表达式 过滤特殊字符 校验所有输入域是否含有特殊符号

楼上2位已经说的很明白了,只允许输入规定的字符,如果输入含有其他字符就直接提示,不允许输入特殊字符,或者直接给它替换掉。

『叁』 如何用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
//大写-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过滤字符

可以使用字符串的match方法匹配出你想要的内容,match支持传入一个正则表达式。

根据你的需求,只需要写一个匹配数字的正则即可,比如var matches="__20__40__43__on__".match(/\d+/g);

得到的matches数组即为符合你要求的内容,不需要split拆分字符串也不需要循环处理,只需要这样简单的一个方法调用即可

参考match方法:http://www.w3school.com.cn/jsref/jsref_match.asp

『伍』 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 如何过滤div里内的指定字符

String.replace(正则表达式,"")
replace是string类型内置的替换方法,第一个参数可以是正则表达式,第二个参数是想要替换成的文本,正则中可以使用/g来表示替换所有匹配的文本,不使用则代表只替换匹配到的第一个字符对象,将第二个参数设为空字符串便可达到过滤的效果。
具体正则需要你自己去了解关于正则的知识了,祝你好运。

『柒』 简单的用js实现过滤多余字符的正则表达式

利用正则表达法除去字符串中的重复字符
str
=
"Google"
str1
=
str.replace(/(.).*\1/g,"$1")
document.write(str
+
"
");
document.write(str1);
[Ctrl+A
全选
注:如需引入外部Js需刷新才能执行]

阅读全文

与js过滤输入字符相关的资料

热点内容
遇见不安全网站怎么办 浏览:251
哪个app有庆余年电视剧 浏览:420
iphone5s视频时很黑 浏览:601
js获取端口号 浏览:347
手机短息发的链接病毒苹果手机 浏览:724
win10专业忘记家庭组密码 浏览:176
南宁applestore几楼 浏览:296
java字符串怎么初始化 浏览:349
医美哪个app好 浏览:768
代码编程和机器人编程哪个好 浏览:875
90版本男法 浏览:796
win10桌面字体难看 浏览:848
三菱fx5u支持哪些编程 浏览:7
优酷在文件夹在哪里 浏览:91
趣打印的数据文件是什么 浏览:871
linuxjava程序 浏览:483
苹果6splus数据线图片 浏览:98
苹果官网数据如何恢复 浏览:251
spss怎么拆分数据 浏览:306
photoshop复位所有工具 浏览:492

友情链接