文明交流,发言的过滤

原创 daijiulong 随笔 jquery 210阅读 2017-05-27 16:42:58 举报

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script>;
</head>
<body>
<input type="text" id="txt">
<input type="button" value="button" id="btn">
</body>
</html>
<script type="text/javascript">
$(function(){

var keywords = "中共,***,wori,傻";
var validKeywords = keywords.split(",");
var validSendMsg = function (msg) {
var upperMsg = msg.toUpperCase();
upperMsg = obj2string(upperMsg);
if (validKeywords != null && validKeywords != "") {
for (var i = 0; i < validKeywords.length; i++) {
var validStr = validKeywords[i];
validStr = validStr.toUpperCase();
validStr = obj2string(validStr);
validStr = trim(validStr);
if (upperMsg.indexOf(validStr) >= 0) {
return false;
}
}
}
return true;
};

function obj2string(o) {
var r = [];
if (typeof o == "string") {
return o.replace(/([\'\"\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t");
}
return o.toString();
}

function trim(str) {
return str.replace(/(^\s)|(\s$)/g, "");
}

function removeAllSpace(str) {
return str.replace(/\s+/g, "");
}

$('#btn').click(function(){
var otxt=removeAllSpace($('#txt').val());
if(validSendMsg(otxt)){
alert('valid')
}else{
alert('invalid')
}
})
})
</script>

评论 ( 0 )
最新评论
暂无评论

赶紧努力消灭 0 回复