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.
Limit the number of characters in a text area...
Moderator: General Moderators
Limit the number of characters in a text area...
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.
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
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.
and body of html page
have fun
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>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>More about "Focus"
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
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
Focus is as ganzina stated and select() will hightligh all the text in the given field.
phpScott
As you are limiting your self to only 100 characters, what about trying
Not as elegant as text area, but it will not accept more than 100 characters, and you don't need any JavaScript.
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.