日期:2014-05-18  浏览次数:20450 次

文本框设置输入的字符串的个数
大家帮我看看这个问题怎么解决啊,我想限制我的文本框输入的数量在100个汉字以内,我看很多人说设置ManLength,但是不管用啊,我用的是多行文本框啊


------解决方案--------------------
方法一:弹出对话框提示
1.html代码

<HTML>
<HEAD>
<title>WebForm6</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function isOver(sText,len)
{
var intlen=sText.value.length;
if (intlen>len)
{
alert("The content length must Less than or Equal "+len);
sText.focus();
sText.select();
}
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="txtName" style="Z-INDEX: 102; LEFT: 200px; POSITION: absolute; TOP: 104px" runat="server"
TextMode="MultiLine" Height="112px" Width="271px"></asp:TextBox>
</form>
</body>
</HTML>2.cs代码
private void Page_Load(object sender, System.EventArgs e)
{
this.txtName.Attributes.Add("onblur","isOver(this,1000);");
}方法二:限制用户继续输入
<HEAD>
<!-- TWO STEPS TO INSTALL LIMIT TEXTAREA:

1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document -->
<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Ronnie T. Moore -->
<!-- Web Site: The JavaScript Source -->

<!-- Dynamic 'fix' by: Nannette Thacker -->
<!-- Web Site: http://www.shiningstar.net -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too longtrim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}
// End -->
</SCRIPT>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
</HEAD>
<BODY>
<!-- textCounter() parameters are: text field, the count field, max length -->
<center>
<form name="myform" action="YOUR-SCRIPT.CGI">
<font size="1" face="arial, helvetica, sans-serif">( You may enter up to 125 
characters. )<br>
<textarea name="message" wrap="physical" cols="28" rows="4" onKeyDown="textCounter(this.form.message,this.form.remLen,125);"
onKeyUp="textCounter(this.form.message,this.form.remLen,125);"></textarea>
<br>
<input readonly type="text" name="remLen" size="3" maxlength="3" value="125">