多行文本框限制输入字符长度(两种方法)
2006-12-19 15:52
337 查看
方法一:弹出对话框提示
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 long
trim 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"> characters
left</font>
</form>
</center>
<p><center>
<font face="arial, helvetica" SIZE="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center>
</BODY>
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 long
trim 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"> characters
left</font>
</form>
</center>
<p><center>
<font face="arial, helvetica" SIZE="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center>
</BODY>
相关文章推荐
- 限制多行文本框输入字符长度(两种方法)
- 多行文本框限制输入字符长度(两种方法)
- 多行文本框限制输入字符长度(两种方法)
- 多行文本框限制输入字符长度(两种方法)
- 多行文本框限制输入字符长度(两种方法)
- 多行文本框限制输入字符长度(两种方法)
- 多行文本框限制输入字符长度(两种方法)
- 多行文本框限制输入字符长度(跳出窗体提示)
- 限制多行文本框的输入长度并显示剩余字符
- Android:限制输入字符个数两种方法
- iOS:UITextField中文输入法输入时对字符长度的限制
- c# 限制textbox的输入范围和长度(长度不用maxlength方法)
- asp.net 多行文本框输入长度限制
- JS限制字符长度及特殊符号不能输入
- iOS:UITextField中文输入法输入时对字符长度的限制
- jQuery实现限制textarea文本框输入字符数量的方法
- 关于android 中EditText 特殊字符过滤和字符长度限制的最优方法
- iOS:UITextField中文输入法输入时对字符长度的限制
- petshop一个防注入,限制字符长度的方法
- iOS:UITextField中文输入法输入时对字符长度的限制