Page 1 of 1
Limit the number of characters in a text area...
Posted: Thu Oct 31, 2002 5:56 am
by rlogin
Hi all,
Has any body tried to limit the number of characters that may be typed into a text area?
An example would be the way
http://www.msn.com tries to limit in any of post your view text area for any article. If i try to type more than say 100 characters in the post your view text area, i get an alert saying you have crossed the limit.
I have been trying to do it but no success so far.
Rgds.
Posted: Thu Oct 31, 2002 6:30 am
by cavey
thats javascript, often used on "send sms" pages, check their source to see how its done :)
Limit the number of characters in a text area...
Posted: Mon Nov 04, 2002 4:18 pm
by ganzina
Hi,
This is the JavaScript code I use to limit a field to 100 characters. I alert the user, truncate the text to the first 100 characters and then go back to the field.
Code: Select all
function doCount(myfield) {
var newVal = myfield;
if (newVal.length > 100) {
alert('This field is limited to 100 characters.');
document.formname.fieldname.value = document.formname.fieldname.value.substring(100);
document.formname.fieldname.focus()
}
}
try this code
Posted: Mon Nov 04, 2002 7:17 pm
by phpScott
the only dif between my code and ganziva is that the fucus() event with blank out the text area where select() will not erease the text.
Code: Select all
<script language=javascript>
function doCount()
{
var f=document.form1;
if (f.fillIn.value.length > 10) {
alert('This field is limited to 100 characters.');
tempString=f.fillIn.value;
f.fillIn.value=tempString.substring(0,10)
f.fillIn.select();
}
}
</script>
and body of html page
Code: Select all
<body>
<form name="form1" method="post" action="" onSubmit="doCount(this)">
<textarea name="fillIn"></textarea><br>
<input type="button" name="test" value="test" onClick="doCount()">
</form>
</body>
have fun
More about "Focus"
Posted: Fri Nov 08, 2002 6:03 pm
by ganzina
Just to clarify..."focus" does not clear out the contents of a form object. Using "focus" allows you to have the field active (outlined) and the cursor goes to the end of the text in the form object.
your right
Posted: Fri Nov 08, 2002 6:37 pm
by phpScott
my javaScript book lied to me about the difference between focus() and select(). Sorry,
Focus is as ganzina stated and select() will hightligh all the text in the given field.
phpScott
Posted: Mon Nov 11, 2002 8:16 am
by f1nutter
As you are limiting your self to only 100 characters, what about trying
Code: Select all
<input type=text size=50 maxlength=100>
Not as elegant as text area, but it will not accept more than 100 characters, and you don't need any JavaScript.