...

View Full Version : how i can make a word limit in textarea



rakibtg
12-22-2010, 04:26 PM
hello, i want to make a word limit on textarea in the below form any body help me- thank you.
please reply me by editing below code


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>freebacklinkcreator.blogspot.com submit form</title>
</head>
<body>

<form method="post" action="sendeail.php">

<!-- DO NOT change ANY of the php sections -->
<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>

<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
<input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
<input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />


Your Name: <br />
<select name="visitor" size="1">
<option value=" Business ">Business </option>
<option value=" Finance ">Finance </option>
<option value=" Reference ">Reference </option>
<option value=" Shopping ">Shopping </option>
<option value=" Arts and Entertainment ">Arts and Entertainment </option>
<option value=" Computers ">Computers </option>
<option value=" Health ">Health </option>
<option value=" News and Media ">News and Media </option>
<option value=" Regional ">Regional </option>
<option value=" Society ">Society </option>
<option value=" Education ">Education </option>
<option value=" Internet ">Internet </option>
<option value=" Recreation ">Recreation </option>
<option value=" Science and Technology ">Science and Technology </option>
<option value=" Sports ">Sports </option>
<option value=" Jobs ">Jobs </option>
<option value=" Online ">Online </option>
<option value=" Money ">Money </option>
<option value=" Affiliate Programs ">Affiliate Programs </option>
</select>
<br />
Your Email:<br />
<input type="text" name="visitormail" size="35" />
<br /> <br />
<br />
Site Title:<br />
<input type="text" name="attn" size="35" />
<br /><br />
Site Description:
<br />
<textarea name="notes" "rows="4" cols="40"></textarea>
<br />
<input type="submit" value="Send Mail" />
<br />
</form>

</body>
</html>

Krupski
12-22-2010, 05:37 PM
hello, i want to make a word limit on textarea in the below form any body help me- thank you.
please reply me by editing below code


OK, give this a try... it doesn't count WORDS, it counts characters since words can be of any length. If you want to actually count words, you can probably modify this code to count spaces... on the assumption that there is one space between each word. The added code is highlighted for your convenience. If you want other than a limit of 100, just change all of the "100" to whatever you want (4 places).



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>freebacklinkcreator.blogspot.com submit form</title>

<script type="text/javascript">
function charCount(text, limit)
{
var e = document.getElementById('remain');
var t = document.getElementById('notes');
var count = limit - text.length;
if (count < 1)
{
t.value = t.value.substring(0, (limit-1));
}
e.innerHTML = 'Characters remaining: ' + count;
}
</script>

</head>
<body>

<form method="post" action="sendeail.php">

<!-- DO NOT change ANY of the php sections -->
<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>

<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
<input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
<input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />


Your Name: <br />
<select name="visitor" size="1">
<option value=" Business ">Business </option>
<option value=" Finance ">Finance </option>
<option value=" Reference ">Reference </option>
<option value=" Shopping ">Shopping </option>
<option value=" Arts and Entertainment ">Arts and Entertainment </option>
<option value=" Computers ">Computers </option>
<option value=" Health ">Health </option>
<option value=" News and Media ">News and Media </option>
<option value=" Regional ">Regional </option>
<option value=" Society ">Society </option>
<option value=" Education ">Education </option>
<option value=" Internet ">Internet </option>
<option value=" Recreation ">Recreation </option>
<option value=" Science and Technology ">Science and Technology </option>
<option value=" Sports ">Sports </option>
<option value=" Jobs ">Jobs </option>
<option value=" Online ">Online </option>
<option value=" Money ">Money </option>
<option value=" Affiliate Programs ">Affiliate Programs </option>
</select>
<br />
Your Email:<br />
<input type="text" name="visitormail" size="35" />
<br /> <br />
<br />
Site Title:<br />
<input type="text" name="attn" size="35" />
<br /><br />
Site Description (100 chars max):
<br />
<textarea id="notes" name="notes" "rows="4" cols="40" onKeyDown="charCount(this.value, 100);" onKeyUp="charCount(this.value, 100);"></textarea>
<br />
<span id="remain">Characters remaining: 100</span>
<br />
<br />
<input type="submit" value="Send Mail" />
<br />
</form>

</body>
</html>
Hope this helps...

-- Roger

P.S. Is this a typo:<form method="post" action="sendeail.php">

Maybe supposed to be "sendemail.php"? :)

Philip M
12-22-2010, 05:55 PM
http://javascript.internet.com/forms/limit-characters-and-words-entered.html

I agree that it is better to limit the number of characters rather than the number of words as words can be of any length and-may-be-run-together-like-this-to-beat-the-limit.

This counts and includes newlines as well as characters.


<script type = "text/javascript">

// works in IE and FF

function countChar(ID){
var txt = ID.value;
var limit = 100;
nChar = ID.value.match(/[^\r\n]/g);
nCRLF = ID.value.match(/\n/g);
if (nChar != null){nChar = nChar.length}
else {nChar = 0}
if (nCRLF != null){nCRLF = nCRLF.length}
else {nCRLF = 0}
nChar = nChar + nCRLF;
if (nChar > limit) {
alert ("You have reached the limit of 400 characters");
txt = txt.substr(0,limit);
ID.value = txt;
}
}

</script>
</head>

<body>
<center>
<textarea cols=32 rows=10 id='area1' onkeyup ="countChar(area1)"></textarea>
</center>
<br>

Quizmaster: What season is said to start on the longest day in December?
Contestant: Spring.
Quizmaster: December, for God's sake!
Contestant: Summer.

Krupski
12-22-2010, 06:13 PM
This counts and includes newlines as well as characters.

Honestly, I think counting newlines is too picky. A person could pre-write the text (say in Notepad or nano) and then paste it into the textarea and squeeze a few extra characters in (since the '\n' are not being counted), but in most cases that's not necessary.

A limit is to maintain control over post size and long winded writers. If someone ends up squeezing in 108 characters in a 100 character limit by using \n, does it really matter?

And, if it DOES matter, then I would say the code receiving the text data is flawed (for example, a buffer expecting 100 characters should be declared with more... say 256 characters, "just in case").

A programmer should plan for the worst case. In this instance, the worst case would probably be a \r\n after each character which could theoretically allow 300 characters posted when the limit is 100. Therefore, the buffer or database cell should be sized for the worst case. IMHO of course... :)

-- Roger

DaveyErwin
12-22-2010, 06:32 PM
Speaking of pasted text, with either solution you still may paste
more text than the "limit"

Krupski
12-22-2010, 06:39 PM
Speaking of pasted text, with either solution you still may paste
more text than the "limit"

You can paset it, but the first keyup or keydown event (in the textarea) will catch it.

Adding an onclick to the submit button might help... and the PHP that the data is being posted to can also check the size and reject it if it's too big.

Computers should have a code activated "slapping hand" attached to the monitor.....



if (count > limit)
{
alert('I said no more than ' + limit + ' characters! Can\'t you read???');
slap();
}


LOL!! :D

Philip M
12-22-2010, 06:41 PM
A programmer should plan for the worst case. In this instance, the worst case would probably be a \r\n after each character which could theoretically allow 300 characters posted when the limit is 100. Therefore, the buffer or database cell should be sized for the worst case. IMHO of course... :)

-- Roger

Yes, that is another approach. But for whatever reason the OP wants to limit the text to some value.

A simplest count is:-


<textarea id = "ta1" rows = "10" cols = "50" onkeyup = "count()">Some Stuff Typed In Here</textarea>

<script type = "text/javascript">
function count() {
var max = 100;
var txt = document.getElementById("ta1").value;
var len = txt.length;
if (len > max) {
alert ("You have reach the limit of " + max + " characters");
document.getElementById("ta1").value = document.getElementById("ta1").value.substring(0,max)
return false;
}
}
</script>

That includes spaces, punctuation and newlines. It may well suffice in many cases.

The big weakness of all these word/character limiting scripts is that the user may paste text in, and then key in no more characters. You can (try to) overcome that by:-



<textarea name = "txt" id = "txt" rows = "20" cols = "75"></textarea>

<script type = "text/javascript">

var txtlngth = 0;
function check4paste() {
var thetext = document.getElementById("txt").value;
var newlen = thetext.length;
if (newlen > (txtlngth + 50)) {
alert ("You appear to have pasted a substantial amount of text into the textarea. This is not allowed!");
document.getElementById("txt").value = thetext.substr(0,txtlngth); // return to previous text
}
else {txtlngth = newlen}

var tim = window.setTimeout("check4paste()", 5000); // check every 5 seconds
}

check4paste();

</script>

And/or, of course, count the number of characters on submit.

jmace
12-22-2010, 07:46 PM
A programmer should plan for the worst case. In this instance, the worst case would probably be a \r\n after each character which could theoretically allow 300 characters posted when the limit is 100. Therefore, the buffer or database cell should be sized for the worst case. IMHO of course... :)


Yes, this is true. But wouldn't it be better to limit it to the size of everything so you don't have more data than is needed/ wanted? Obviously JavaScript is not the way to really limit data sizes, as I can disable JavaScript any time I want, but still. Why would you ever allow more than you want? IMHO, there really doesn't need to be a margin of error if you carefully validate everything-- which you should always do.

I'm still learning, so this is all my opinion and counts for next to nothing, lol.

rakibtg
12-22-2010, 07:56 PM
woo thank you everybody for such a help this script will be hosted in my free backlink creator blog (http://freebacklinkcreator.blogspot.com/) where you mostly welcome. it is unpublished but it will be publish after some days.

Old Pedant
12-22-2010, 08:05 PM
Personally, I see no reason at all to muck with keyup/keydown. Just do it all via a timer.



<script type="text/javascript">
function limit( )
{
var ta = document.forms[0].notes;
var words = ta.value.replace(/\n/g," \n").split( /[ ]/g );
if ( words.length > 100 )
{
ta.value = words.slice( 0, 100).join(" ");
}
}
setInterval( limit, 250 ); // do this every quarter of a second
</script>

Presto. Done. It puts a space in front of every newline so that the split and join on space will work. It's not perfect, as it would count a sequence of 5 spaces as 4 words. All that can be fixed easily, if it's important.

Krupski
12-22-2010, 08:05 PM
woo thank you everybody for such a help this script will be hosted in my free backlink creator blog (http://freebacklinkcreator.blogspot.com/) where you mostly welcome. it is unpublished but it will be publish after some days.

Please forgive me for asking, but are you a native English speaker?

The grammar and spelling on your site is... how can I put it... not good.

Your front page screams (either) "Foreigner who can't write proper English" or "Teen kid who failed English class a few times".

Please don't take this the wrong way... hopefully you will see it as constructive criticism...

-- Roger

(edit to add): I'll be happy to give you a "clean native English" re-write of your text if you need it.

Philip M
12-22-2010, 08:25 PM
Personally, I see no reason at all to muck with keyup/keydown. Just do it all via a timer.



<script type="text/javascript">
function limit( )
{
var ta = document.forms[0].notes;
var words = ta.value.replace(/\n/g," \n").split( /[ ]/g );
if ( words.length > 100 )
{
ta.value = words.slice( 0, 100).join(" ");
}
}
setInterval( limit, 250 ); // do this every quarter of a second
</script>

Presto. Done. It puts a space in front of every newline so that the split and join on space will work. It's not perfect, as it would count a sequence of 5 spaces as 4 words. All that can be fixed easily, if it's important.

Is not the weakness with this is that the script is running all the time, and consuming resources, whether text is being entered or not? Presumably you mean
setTimeout(limit,250); // do this every quarter of a second

Old Pedant
12-22-2010, 08:33 PM
No, I meant setInterval.

Sure it's running all the time. So what. Granted, I wouldn't use this on a page where there are tons of objects being moved around (say a video game), but for a simple <form> entry page?? Why not. The client computer will never notice this tiny amount of over head. I doubt that it eats up 100 microseconds every 250 milliseconds. Even if it's 500 microseconds, it's only eating up 2% of the CPU time.

Krupski
12-22-2010, 08:55 PM
No, I meant setInterval.

Sure it's running all the time. So what. Granted, I wouldn't use this on a page where there are tons of objects being moved around (say a video game), but for a simple <form> entry page?? Why not. The client computer will never notice this tiny amount of over head. I doubt that it eats up 100 microseconds every 250 milliseconds. Even if it's 500 microseconds, it's only eating up 2% of the CPU time.

Honestly, my first thought was "watch it with a timer" but then I worried about wasting CPU cycles and instead used the keyup/keydown approach.

Most of the programming I do is on a small (68HC11) microcontroller running at 2 MHz... so every CPU cycle is important to me. :)

TinyScript
12-22-2010, 09:00 PM
slapping hand! I LOL'd



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum