Churchill27
30-01-2002, 10:10
Hi,
bin jetzt ausgeschlafen und habe doch noch eine Frage ...
... kann ich in einer Textarea die Zeilenanzahl begrenzen ???
Quellcode sieht so aus:
<textarea onkeyup=checkdigits(); name="message" rows="2" wrap="VIRTUAL" cols="50"></textarea>
Nur, wenn ich jetzt mehr als zwei Zeilen eingebe, kann man vertikal scrollen. Kann man das abschalten ???
Überprüft mit javascript ob value > 100 (99 ist vielleicht besser..mußt du mal probieren) ist und schneide in dem fall alle Zeichen die mehr sind ab.
Probier es mal so:
<script language=javascript>
function CheckLen(Target)
{
MaxLength = 100;
if (Target.value.length > MaxLength)
document.MyForm.MyText.value =
document.MyForm.MyText.value.substr(0,MaxLength);
}
</script>
<form name=MyForm>
<textarea rows=5 cols=30 name=MyText
onChange=CheckLen(this)
onBlur=CheckLen(this) onFocus=CheckLen(this)
onKeyDown=CheckLen(this) onKeyUp=CheckLen(this)></textarea>
<input type=submit name=MySubmit value=OK>
</form>
edit: Umbrüche
Die manuellen Zeilenumbrüche müssen doch auch noch gezählt werden ... es geht hier um Zeilenzahl, nicht um Zeichen!
<script language=javascript>
function CheckLen(Target, maxlines)
{
res = true;
cols = Target.cols;
val = Target.value;
lines = 1;
col = 0;
for (k=0; res && (k<val.length); k++)
if (col==cols || '\n'==val[k])
{
res = (++lines<=line)
col = 0;
}
if (!res)
document.MyForm.MyText.value =
document.MyForm.MyText.value.substr(0,k);
}
</script>
<form name="MyForm" onSubmit="CheckLen(this.MyText, 2);">
<textarea rows="5" cols="30" name="MyText"
onChange="CheckLen(this, 2);"
onBlur="CheckLen(this, 2);" onFocus="CheckLen(this, 2);"
onKeyDown="CheckLen(this, 2);" onKeyUp="CheckLen(this, 2);"
></textarea>
<input type="submit" name="MySubmit" value="OK">
</form>