最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Limiting Html Textarea - Stack Overflow

programmeradmin2浏览0评论

I'm having a textarea

<textarea name="Users[address]" id="Users_address"></textarea> 

Now I do not want the user the enter more than 25 characters per line and not more than 3 lines, is this possible to validate and achieve and how? The validation can be done in javascript or php

I'm having a textarea

<textarea name="Users[address]" id="Users_address"></textarea> 

Now I do not want the user the enter more than 25 characters per line and not more than 3 lines, is this possible to validate and achieve and how? The validation can be done in javascript or php

Share Improve this question asked Mar 16, 2011 at 8:32 ElitmiarElitmiar 37k77 gold badges182 silver badges233 bronze badges 5
  • I do not want the user the enter more than 25 characters per line. Please define by what you mean by line. Is it related to enter key press? – rahul Commented Mar 16, 2011 at 8:37
  • The thing is that in Chrome user can adjust the size of textareas, so your line concept might not work there. – rahul Commented Mar 16, 2011 at 8:38
  • @rahul - In a textarea there can be more that one line of text entered by user, per line it should not be more thatn 25chars, and only 3lines allowed – Elitmiar Commented Mar 16, 2011 at 8:48
  • Are you differentiating lines using \n character? – rahul Commented Mar 16, 2011 at 9:03
  • @rahul - yes by the \n character – Elitmiar Commented Mar 16, 2011 at 19:06
Add a ment  | 

7 Answers 7

Reset to default 7

jQuery

As the user types, this will remove text which doesn't match the rules.

var maxLines = 2,
    maxLineWidth = 5;

$('#Users_address').bind('change keyup paste drop', function() {
   var value = $(this).val(),
       lines = value.split('\n'),
       linesLength = lines.length;
    
    if (linesLength > maxLines) {
       lines = lines.slice(0, maxLines);
       linesLength = maxLines;
    }
       
    for (var i = 0; i < linesLength; i++) {
        if (lines[i].length > maxLineWidth) {
          lines[i] = lines[i].substring(0, maxLineWidth);  
        } 
    }
    
    $(this).val(lines.join('\n'));
});

jsFiddle.

PHP

Where $str is your user inputted string.

define('MAX_LINES', 10);
define('MAX_LINE_LENGTH', 25);

$lines = explode("\n", $str);

if (count($lines) > MAX_LINES) {
    echo 'Too many lines.';
}

foreach($lines as $line) {
    if (strlen($line) > MAX_LINE_LENGTH) {
        echo 'Too many chars wide';
        break;
    }
}

This will reject text which doesn't match the rules. To turn any text into text that follows the rules (possibly dropping characters), just convert the jQuery above to PHP.

Insert the following code into the page head:

<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    } else {
        limitCount.value = limitNum - limitField.value.length;
    }
}
</script>

Use the following code to create the form and text area (if necessary, change the name of the form and text area to suit your needs):

<form name="myform">
<textarea name="limitedtextarea" onKeyDown="limitText(this.form.limitedtextarea,this.form.countdown,100);" 
onKeyUp="limitText(this.form.limitedtextarea,this.form.countdown,100);">
</textarea><br>
<font size="1">(Maximum characters: 100)<br>
You have <input readonly type="text" name="countdown" size="3" value="100"> characters left.</font>
</form>

To create a single-line text field instead of a text area, use the following code:

<form name="myform">
<input name="limitedtextfield" type="text" onKeyDown="limitText(this.form.limitedtextfield,this.form.countdown,15);" 
onKeyUp="limitText(this.form.limitedtextfield,this.form.countdown,15);" maxlength="15"><br>
<font size="1">(Maximum characters: 15)<br>
You have <input readonly type="text" name="countdown" size="3" value="15"> characters left.</font>

Reference form:

http://www.mediacollege./internet/javascript/form/limit-characters.html

must be done with javascript and php both...

Try this mootools class which can limit the character limit in text area,

http://www.xpertdeveloper./2010/11/text-limiter-class-with-mootools/

Check this helpful tutorial from Matt.

You cannot do it natively with html. Maybe with javascript. What you can do is post process the data. You can check each line's length and number of lines and return an error when user exceeds limits.

Use explode() on the entry with "\n" as the delimiter split the entry into lines in an array. Use count on the array to check the number of lines. You can then use strlen on each index to check the length of the line.

Try out this code!

<html>
<title>Limit Charecters</title>
<head>
<script language="javascript" src="Jquery.js"></script>
<script language="javascript">
function limitChars(textid, limit, infodiv)
{
 var text = $('#'+textid).val(); 
 var textlength = text.length;
 if(textlength > limit)
 {
  $('#' + infodiv).html('You cannot write more then '+limit+' characters!');
  $('#'+textid).val(text.substr(0,limit));
  return false;
 }
 else
 {
  $('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
  return true;
 }
}
</script>
</head>
<body>
       <textarea name="Users[address]" id="Users_address" onkeyup="limitChars(this, 20, 'charlimitinfo')">
       </textarea>    
</body>
</html>

Hope it works!

Many people have answered with Javascript validation. In php, you should always validate input.

if (strlen($_POST['users\[address\]') > 25)
{
     // The input was larger than 25, so produce error

   echo 'Your input is longer than 25 characters. Please retry.';
}
else
{
   echo 'Your post was successful.';

     // Perform whatever you want using the data from the form
}
发布评论

评论列表(0)

  1. 暂无评论