簡體   English   中英

限制textarea行服務器端(php)和HTML?

[英]Limit textarea lines server side (php) and in html?

我已經嘗試了多種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() ) );

你可以試試這個

<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>

復制粘貼適用於此...但適用於更改事件...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM