TextArea Character Limit

@

@sh

Guys,

Working on a function to alert the user to too many characters being entered
into a text area, I've put together this function so far borrowing bits from
resource websites...

function
Ash_LimitMaxTextAreaCharacters(TheMaxCharacters,TheFriendlyFormName) {
if(document.myform.box.value.length > TheMaxCharacters) {
alert('Sorry but '+TheFriendlyFormName+' contains too many characters,
please remove '+
(document.myform.box.value.length - TheMaxCharacters)+ ' characters and
try again');
return false; }
else
return true; }

However I have many text areas throughout the site and wish to reuse this
function from a central include file, therefore I need to amend the function
to allow me to pass the name of the text area and the form that its within,
therefore how would it be amended and what would be the string to call it
via the Text area tag?

Something like...?

<textarea name="CampaignMailTextIntro" cols="125" rows="9"
id="CampaignMailTextIntro" onChange="Ash_LimitMaxTextAreaCharacters(50,'The
Intro Text Form');"></textarea>

Appreciate your help!!

Cheers, Ash
 
@

@sh

Ok, well so far I've managed to get as far now as...

function
Ash_LimitMaxTextAreaCharacters(TheMaxCharacters,TheFriendlyFormName,TheObject)
{
if(document.getElementById(TheObject).value.length > TheMaxCharacters) {
alert('Sorry but '+TheFriendlyFormName+' contains too many characters,
please remove ' + (document.getElementById(TheObject).value.length -
TheMaxCharacters) + ' characters and try again');
return false; }
else
return true;
}

AND IN THE BODY...

<textarea name="CampaignMailTextIntro" cols="125" rows="9"
id="CampaignMailTextIntro"
onChange="javascript:Ash_LimitMaxTextAreaCharacters('10','Introduction
Text',this);"></textarea>

But each time I make a change to the text area, I just get an error 'Object
not found', I'm sure I've done something really basic wrong here...can
someone help me but WITHOUT rewriting the code? I just need to know whats
wrong in this example?

Appreciate your help!

Cheers, Ash
 
M

MPTurner

Ash,

Since you're passing in the object--not the object name--you can
directly reference the object. No need to use the getElementById
method.

function
Ash_LimitMaxTextAreaCharacters(TheMaxCharacters,TheFriendlyFormName,TheObject)
{
if(TheObject.value.length > TheMaxCharacters) {
alert('Sorry but '+TheFriendlyFormName+' contains too many
characters, please remove ' + (TheObject.value.length -
TheMaxCharacters) + ' characters and try again');
return false; }
else
return true;
}

--MPTurner
 
@

@sh

Many thanks for your help MPTurner, I've tried that but getting an 'Object
Expected' error when it hits the specified character limit, here's what I'm
using within the TextArea tag...

onKeyUp="javascript:Ash_LimitMaxTextAreaCharacters('10','Introduction
Text',this)"

What do you think?

Cheers, Ash
 
@

@sh

Sorry, correction, tried this but its causing a Javascript error as soon as
I type into the TextArea, i.e. as soon as the function runs.

Any ideas?

Cheers, Ash
 
R

RobG

@sh said:
Guys,

Working on a function to alert the user to too many characters being entered
into a text area, I've put together this function so far borrowing bits from
resource websites...

Provide on-screen help to indicate the maximum number of characters.
Show how many they have left, let them know when they've entered too
many. That way at least if scripting is not enabled, the user still
knows how many they can enter even though they'll have to count themselves.

Some scripts also truncate extra characters, I don't like that. I'd
rather let the user enter what they want, then trim it down to fit.

e.g.


<style type="text/css">
.screenHelp {
font-family: arial, sans-serif;
font-size: 90%;
color: #FF6600;
background-color: #ffffff;
}
</style>

<script type="text/javascript">

function checkMaxChars(el, num)
{
var msg;
var elName = el.name || el.id;
var msgEl = document.getElementById(elName + '-msg')
var count = num - el.value.length;
if (msgEl){
if (count == num){
msg = 'Maximum ' + num + ' characters';
} else if (count > 0){
msg = count + ' character'
+ ((count > 1)?'s':'')+ ' left';
} else if (0 == count){
msg = 'Limit reached';
} else {
msg = '<b>Too many characters</b>, please remove '
+ (count * -1);
}
msgEl.innerHTML = msg;
}
}
</script>

<textarea id="t-01" onkeyup="checkMaxChars(this, 5)";
"></textarea><span id="t-01-msg" class="screenHelp">Maximum
5 characters</span>




[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top