【问题标题】:Limit textarea lines server side (php) and in html?限制 textarea 行服务器端(php)和 html?
【发布时间】:2014-09-19 05:22:02
【问题描述】:

我尝试了多种 javascript 和 jquery 方法来限制行,但大多数情况下要么存在错误,要么存在复制/粘贴等问题。请让我知道它应该如何用于客户端..大部分是从复制/粘贴中修补的,如果您不按 Enter 键但继续写入等,则限制附加行...

另外,这只是我的服务器端检查,所以如果我也可以改进它,请告诉我?

$lines = array_slice(explode("\n", trim( $_POST['description'])), 0, 10); // max 10 lines

foreach ($lines as $key => $value)
{
    $lines[$key] = substr(trim($value), 0, 100); // max 100 chars
}

$insert['teams_descr'] = implode("\n", $lines);
                $this->db->update( 'teams', $insert, array( 'teams_id' => $this->user->leader_team_id() ) );

【问题讨论】:

  • 已经有帖子描述了这个stackoverflow.com/questions/14259580/…
  • 感谢您提供链接,但不幸的是我已经检查过了。这两个示例都有错误,例如允许复制/粘贴以及我在主帖中已经提到的其他内容。

标签: javascript php jquery codeigniter textarea


【解决方案1】:

你可以试试这个

<textarea id="splitLines" rows="10" cols="100" onchange="ValidateLines(this.value)"></textarea>
<script>
function ValidateLines(val) {
    var lines = val.split("\n");
    for (var i = 0; i < lines.length; i++) {
    if (lines[i].length <= 100) continue;
    var j = 0; space = 100;
    while (j++ <= 100) {
      if (lines[i].charAt(j) === " ") space = j;
    }
    lines[i + 1] = lines[i].substring(space + 1) + (lines[i + 1] || "");
    lines[i] = lines[i].substring(0, space);
    }
    document.getElementById("splitLines").value = lines.slice(0, 10).join("\n");

}
</script>

复制粘贴适用于此...但它正在处理更改事件...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-08
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    相关资源
    最近更新 更多