ASP multine textbox maxlength

liunx

Guest
I am using ASP.NET in Visual Studio .NET, I am making a form that contains a comments box. For that I used a multiline textbox. My problem is that I want it to have a maximum of 250 characters but maxlength does not apply to multiline texbox. How can I fix it?I'm quite sure that you cannot use maxlength with a <textarea> field.

What you can do is put in some client side code (javascript/vbscript) to check the length of the field and verify that it's not over a certain limit.


<head>
<script language=vbscript>
Function verifyForm()
dim formItem
dim blnFormIsValid
dim strError

blnFormIsValid = true

for each formItem in document.all.tags("textarea")
if len(formItem.value) > 250 then
blnFormIsValid = false
if strError = "" then
strError = formItem.name & " contains too many characters!"
else
strError = strError & vbCrLf & formItem.name & " contains too many characters!"
end if
end if
next

if blnFormIsValid
document.myForm.submit
else
alert(strError)
end if
End Function
</script>
</head>
<body>
<form name="myForm" action="nextpage.asp">
<textarea name="textBox1" cols=25 rows=5></textarea><br>
<textarea name="textBox2" cols=25 rows=5></textarea><br>
<textarea name="textBox3" cols=25 rows=5></textarea><br>

<a href=http://www.htmlforums.com/archive/index.php/"vbscript: verifyForm()">Submit Form</a>

</form>
</body>use this little script:


<script language="JavaScript">
function count()
{
var iMaxLength = 248;

if (document.form1.CommentBox.value.length > iMaxLength)
document.form1.CommentBox.value = document.form1.CommentBox.value.substring(0, iMaxLength);

Counter.innerHTML = document.form1.CommentBox.value.length;
}
</script>


putts textarea doesn't really exsist in .NET. You would use something like this:


<asp:TextBox id="TextBox1" runat="server" Width="442px" Height="207px" TextMode="MultiLine" </asp:TextBox>yet another solution...


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Ronnie T. Moore -->
<!-- Web Site: The JavaScript Source -->

<!-- Dynamic 'fix' by: Nannette Thacker -->
<!-- Web Site: <!-- m --><a class="postlink" href="http://www.shiningstar.net">http://www.shiningstar.net</a><!-- m --> -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! <!-- m --><a class="postlink" href="http://javascript.internet.com">http://javascript.internet.com</a><!-- m --> -->

<!-- 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>
</head>
<body>
<form name=form1>
<textarea name=content cols=70 rows=18 onKeyDown="textCounter(this.form.content,this.form.remLen,4000);"
onKeyUp="textCounter(this.form.content,this.form.remLen,4000);"
onFocus="textCounter(this.form.content,this.form.remLen,4000);">
</textarea>
<br><br>
<input readonly type=text name=remLen size=2 maxlength=3 value="4000" style="border: 1px;"> characters left</font>
</form>
</body>
</html>
 
Back
Top