14862875 Posted May 18, 2009 Share Posted May 18, 2009 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 More sharing options...
Ken2k7 Posted May 18, 2009 Share Posted May 18, 2009 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 More sharing options...
waynew Posted May 18, 2009 Share Posted May 18, 2009 Make sure that you also check the string length on the server side of things as well as users can easily disable JavaScript and thus, render your little block useless. if(strlen(trim($_POST['message'])) > 100){ //show error or whatever { Link to comment https://forums.phpfreaks.com/topic/158648-solved-textarea/#findComment-836807 Share on other sites More sharing options...
mrMarcus Posted May 18, 2009 Share Posted May 18, 2009 then use strlen to verify the length of the textarea for anybody who has javascript turned off, ie. <?php if (strlen($_POST['text']) > 100) { //do something; } ?> man, you beat me to it .. posting it anyways Link to comment https://forums.phpfreaks.com/topic/158648-solved-textarea/#findComment-836808 Share on other sites More sharing options...
Brian W Posted May 18, 2009 Share Posted May 18, 2009 counting words, not strlen... right? $words = count(explode(" ", $_POST['text'])); if($words > 100){ //fail code }else { //other code just a thought Link to comment https://forums.phpfreaks.com/topic/158648-solved-textarea/#findComment-836811 Share on other sites More sharing options...
waynew Posted May 18, 2009 Share Posted May 18, 2009 What if the user enters a word that is 4000 characters long? They'll surely break the entire Internet. Btw, yes I did notice that my code would cough up an error. But that was only put there to fool you guys into thinking that I wasn't perfect. Link to comment https://forums.phpfreaks.com/topic/158648-solved-textarea/#findComment-836814 Share on other sites More sharing options...
mrMarcus Posted May 19, 2009 Share Posted May 19, 2009 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 More sharing options...
14862875 Posted May 19, 2009 Author Share Posted May 19, 2009 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 More sharing options...
mrMarcus Posted May 19, 2009 Share Posted May 19, 2009 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.