Jump to content

[SOLVED] Textarea


14862875

Recommended Posts

How do i limit the amount of words entered into a textarea to 100 words?

 

<form name='cloud_constructor' method='post' action='index.php'>

<table cellspacing='10px'>
<tr><td style="vertical-align:top;color:white;font-family:tahoma;font-size:12;">Please enter some text and click submit to create a cloud: </td><td><textarea name='text' rows='10' cols='30'></textarea>

</table>
<input type="hidden" name="submitted" value="TRUE"/>
<input type='submit' value='Submit'/>
</form>

Link to comment
https://forums.phpfreaks.com/topic/158648-solved-textarea/
Share on other sites

I think you need JavaScript.

 

Give your form an onsubmit attribute such as -

<form name='cloud_constructor' method='post' action='index.php' onsubmit='return validate(this);'>

<table cellspacing='10px'>
<tr><td style="vertical-align:top;color:white;font-family:tahoma;font-size:12;">Please enter some text and click submit to create a cloud: </td><td><textarea name='text' rows='10' cols='30'></textarea>

</table>
<input type="hidden" name="submitted" value="TRUE"/>
<input type='submit' value='Submit'/>
</form>

 

Then with the validate function, you can check the length.

<script type="text/javascript">
function validate (frm) {
     return frm.text.value.split(' ').length <= 100;
}
</script>

 

In that JavaScript function, it checked if the word length inside the textarea is less than or equal to 100. It returns a boolean value. So if the textarea has more than 100 words, it returns false and the form is not submitted.

 

Hope this helps.

Link to comment
https://forums.phpfreaks.com/topic/158648-solved-textarea/#findComment-836701
Share on other sites

man, i should read a little more carefully .. it's a word limit you want...

<?php
//turn into array;
$words = explode (' ', $_POST['text']);
//can lose the last word .. especially effective if also using a character allowance;
$discard = array_pop ($words);
// how many words are allowed in description output;
$word_limit = 100;
// reassemble text;
$words = implode (' ', array_slice($words, 0, $word_limit));
?>

 

try that;

Link to comment
https://forums.phpfreaks.com/topic/158648-solved-textarea/#findComment-836824
Share on other sites

Thanx guys helps a lot..slight problem

 

<?php 

if(isset($_POST['submitted']))
{

if(empty($_POST['text'])) OR ($_POST['text'])) <100
{
print "<span style='color:red;font-weight:bold;'>Please enter some text!!</span><p/>";
include('form.php');

}

else
{
$cloud_one=$_POST['text'];
$cloud_one=strtolower(stripslashes($cloud_one));
$cloud_one=count(explode(' ',$cloud_one));

if ($cloud_one<100){
print"You must enter atleast 100 words!";

}
else {print" Thanks for entering the required amount of words";

}

$cloud_one=str_replace('[^a-zA-Z ]+', '', $cloud_one);
$stopwords=array("a", "about", "above", "above", "across", "after", "afterwards", "again", "against", "all", "almost", "alone", "along", "already", "also","although","always","am","among", "amongst", "amoungst", "amount", "an", "and", "another", "any","anyhow","anyone","anything","anyway", "anywhere", "are", "around", "as", "at", "back","be","became", "because","become","becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", "beside", "besides", "between", "beyond", "bill", "both", "bottom","but", "by", "call", "can", "cannot", "cant", "co", "con", "could", "couldnt", "cry", "de", "describe", "detail", "do", "done", "down", "due", "during", "each", "eg", "eight", "either", "eleven","else", "elsewhere", "empty", "enough", "etc", "even", "ever", "every", "everyone", "everything", "everywhere", "except", "few", "fifteen", "fify", "fill", "find", "fire", "first", "five", "for", "former", "formerly", "forty", "found", "four", "from", "front", "full", "further", "get", "give", "go", "had", "has", "hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon", "hers", "herself", "him", "himself", "his", "how", "however", "hundred", "ie", "if", "in", "inc", "indeed", "interest", "into", "is", "it", "its", "itself", "keep", "last", "latter", "latterly", "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might", "mill", "mine", "more", "moreover", "most", "mostly", "move", "much", "must", "my", "myself", "name", "namely", "neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none", "noone", "nor", "not", "nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only", "onto", "or", "other", "others", "otherwise", "our", "ours", "ourselves", "out", "over", "own","part", "per", "perhaps", "please", "put", "rather", "re", "same", "see", "seem", "seemed", "seeming", "seems", "serious", "several", "she", "should", "show", "side", "since", "sincere", "six", "sixty", "so", "some", "somehow", "someone", "something", "sometime", "sometimes", "somewhere", "still", "such", "system", "take", "ten", "than", "that", "the", "their", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "thereupon", "these", "they", "thickv", "thin", "third", "this", "those", "though", "three", "through", "throughout", "thru", "thus", "to", "together", "too", "top", "toward", "towards", "twelve", "twenty", "two", "un", "under", "until", "up", "upon", "us", "very", "via", "was", "we", "well", "were", "what", "whatever", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither", "who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would", "yet", "you", "your", "yours", "yourself", "yourselves", "the"); 
$cloud_one=preg_replace('/\b('.implode("|",$stopwords).')\b/i', '', $cloud_one);


$wordCount = array_count_values($cloud_one);

print "<pre>";
	print_r($wordCount);
print "</pre>";


}
}

else
{
include('form.php');

} 

 

The count function clashes with the array_count_values function..how do i fix that

Link to comment
https://forums.phpfreaks.com/topic/158648-solved-textarea/#findComment-837048
Share on other sites

why, what's the error?

 

and this line shouldn't be working :

if(empty($_POST['text'])) OR ($_POST['text'])) <100

 

assuming you had the <100 within the condition, what is it supposed to be doing anyways?  is what less than 100?  the string length?  the number of something?  you need to declare that.

 

besides that, having a check on whether the word count is less than 100 also checks if it is empty (assuming your value is returning 0, which is less than 100), making your (empty($_POST['text'])) unnecessary.

Link to comment
https://forums.phpfreaks.com/topic/158648-solved-textarea/#findComment-837181
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.