`

js特殊字符的过滤

    博客分类:
  • JS
 
阅读更多

//特殊字符过滤
function replaceCharacter(){
 var inputs = document.getElementsByTagName("input");
 if(inputs){
  for(var i=0;i<inputs.length;i++){
   var input = inputs[i];
   if((input.type=="text" || input.type=="password") && !input.onchange){
    input.onchange=function(){
     this.value=replaceall(this.value);
    }
   }
  }
 }
}

function replaceall(value){
 var str = value;
 value=str.replace("$","").replace("%","").replace("\*","").replace("(","").replace(")","").replace("!","").replace("@","").replace("#","").replace("\&","").replace("\^","").replace("'","").replace("\"","").replace(";","").replace("<","").replace(">","").replace("?","");
 if(str != value){
  value = replaceall(value);
 }
 return value;
}
document.ready=function(){
 replaceCharacter();
}
function showKeyPress(evt) {
 evt = (evt) ? evt : window.event
 return checkSpecific(String.fromCharCode(evt.keyCode));
}
function checkSpecific(realkey){
 var specialKey = "!@<>#$()?%\&\^*\'\"\+\|";//特殊字符列表
 var flg = false;
 flg = (specialKey.indexOf(realkey) >= 0);
 if (flg) {
  return false;
 }
 return true;
}
document.onkeypress=showKeyPress;

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics