asp.net非法文字提示
要求当用户在文本框输入文字后,在点击提交的时候判断内容中是否包含一些非法文字,如果有的话进行提示,该怎么实现呢?网上都是进行替换的!请高手解答!!!...
要求当用户在文本框输入文字后,在点击提交的时候判断内容中是否包含一些非法文字,如果有的话进行提示,该怎么实现呢?网上都是进行替换的!请高手解答!!!
展开
2个回答
展开全部
<HTML>
<HEAD>
<TITLE>Temp</TITLE>
</HEAD>
<BODY>
<input type="text" id="UserName" value="你是我的好朋友,他不是" />
<input type="button" value="提交" id="Save" />
<br/>
<span id="error"></span>
<script>
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
var TextFilter = function(ClassName)
{
this.ClassName = ClassName;
this.FilterList = null;
}
TextFilter.prototype.InitFilter = function()
{
if(this.FilterList == null)
{
this.FilterList = [
"我的",
"你",
"我",
"他"
];
}
}
TextFilter.prototype.Init = function()
{
this.InitFilter();
var TextObj = document.getElementById("UserName");
var Text = TextObj.value.Trim();
if(Text == "")
{
alert("内容不能为空");
TextObj.focus();
return;
}
var ErrorList = [];
for(var i=0;i<this.FilterList.length;i++)
{
if(Text.indexOf(this.FilterList[i]) != -1)
{
var reg = new RegExp(this.FilterList[i],"g");
Text = Text.replace(reg,"<font color=\"red\">"+ this.FilterList[i] +"</font>");
ErrorList.push(this.FilterList[i]);
}
}
document.getElementById("error").innerHTML = Text;
alert("您输入的字符串中包含特殊字符:\r\n[" + ErrorList.join(',') + "]");
}
var MyTextFilter = new TextFilter("MyTextFilter");
var Obj = document.getElementById("Save");
Obj.onclick = new Function(""+MyTextFilter.ClassName+".Init();");
</script>
</BODY>
</HTML>
要是还有啥不明白的,就HI我吧
<HEAD>
<TITLE>Temp</TITLE>
</HEAD>
<BODY>
<input type="text" id="UserName" value="你是我的好朋友,他不是" />
<input type="button" value="提交" id="Save" />
<br/>
<span id="error"></span>
<script>
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
var TextFilter = function(ClassName)
{
this.ClassName = ClassName;
this.FilterList = null;
}
TextFilter.prototype.InitFilter = function()
{
if(this.FilterList == null)
{
this.FilterList = [
"我的",
"你",
"我",
"他"
];
}
}
TextFilter.prototype.Init = function()
{
this.InitFilter();
var TextObj = document.getElementById("UserName");
var Text = TextObj.value.Trim();
if(Text == "")
{
alert("内容不能为空");
TextObj.focus();
return;
}
var ErrorList = [];
for(var i=0;i<this.FilterList.length;i++)
{
if(Text.indexOf(this.FilterList[i]) != -1)
{
var reg = new RegExp(this.FilterList[i],"g");
Text = Text.replace(reg,"<font color=\"red\">"+ this.FilterList[i] +"</font>");
ErrorList.push(this.FilterList[i]);
}
}
document.getElementById("error").innerHTML = Text;
alert("您输入的字符串中包含特殊字符:\r\n[" + ErrorList.join(',') + "]");
}
var MyTextFilter = new TextFilter("MyTextFilter");
var Obj = document.getElementById("Save");
Obj.onclick = new Function(""+MyTextFilter.ClassName+".Init();");
</script>
</BODY>
</HTML>
要是还有啥不明白的,就HI我吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询