Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-06-2008, 01:04 AM   PM User | #1
florida
Regular Coder

 
Join Date: Jul 2002
Posts: 362
Thanks: 0
Thanked 0 Times in 0 Posts
florida is an unknown quantity at this point
Max size of characters allowed to type in textarea

For a textarea how do I limit the number of characters in the box to 500?

For input text I can set to maxlength and limit the entry in the input: <input type="text" maxlength="500" name="myfield">
For textarea I assume I need to create a validation or something to fetch the length of the field and stop the entry after 500 characters. Please advise how this can be done.
florida is offline   Reply With Quote
Old 05-06-2008, 03:43 AM   PM User | #2
binaryWeapon
Regular Coder

 
Join Date: Sep 2007
Location: AZ, USA
Posts: 685
Thanks: 6
Thanked 46 Times in 46 Posts
binaryWeapon is on a distinguished road
A quick Google search turned up this: http://www.mediacollege.com/internet...haracters.html.
binaryWeapon is offline   Reply With Quote
Old 05-07-2008, 02:22 AM   PM User | #3
florida
Regular Coder

 
Join Date: Jul 2002
Posts: 362
Thanks: 0
Thanked 0 Times in 0 Posts
florida is an unknown quantity at this point
Thanks it works but I also want a alert message and cant seem to get it to work because not alert message appears once the 500 characters is entered.
Please advise:
Code:
<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
                                alert("You are trying to enter more than 500 characters");
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}
</script>
----------

<form name="myform">
<textarea name="limitedtextarea" onKeyDown="limitText(this.form.limitedtextarea,this.form.countdown,500);" 
onKeyUp="limitText(this.form.limitedtextarea,this.form.countdown,500);">
</textarea>
florida is offline   Reply With Quote
Old 05-07-2008, 08:03 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Over-complicated. Try this:-

<script type="text/javascript">

function limitText(limitField) {
fldValue = limitField.value;
var limitNum = 500;
var chars = limitNum - fldValue.length;
alert (chars); // delete after testing
if (chars <= 0) {
alert ("You are trying to enter more than the limit of " + limitNum + " characters! ");
fldValue = fldValue.substring(0,limitNum-1)
document.myform.limitedtextarea.value = fldValue;
}
if (chars == 20) {
alert ("You are approaching the limit of " + limitNum + " characters and have only 20 characters left! ")
}
}

</script>

<form name="myform">
<textarea name="limitedtextarea" onKeyDown="limitText(limitedtextarea)";
onKeyUp="limitText(limitedtextarea)"></textarea>
</form>
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:04 PM.


Advertisement
Log in to turn off these ads.