0

I've tried multiple javascript and jquery methods for limiting the lines but mostly either there is a bug on it or there is an issue with copy/paste and etc. Please let me know how it should be for client side .. mostly patched from copy/paste, restrict additional line if you dont press enter but keep writing and etc ...

Also, that's my server side check only at the moment, so please let me know if i can improve it as well?

$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() ) );
MobEn
  • 65
  • 9
  • There is already a post describing this http://stackoverflow.com/questions/14259580/textarea-with-limited-lines-and-char-limits – Web Sticklers Sep 19 '14 at 05:36
  • Thanks for providing link for it but unfortunately I've already checked it. Both examples have bugs such as copy/paste allowed and additional stuff I've mentioned already in my main post. – MobEn Sep 19 '14 at 05:42

1 Answers1

0

You can try this

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

copy paste works on this... but it is working on change event...