1

i need functionality that every character after 5th line in textarea will get deleted , and 5th line will only contain max 15 chars.

i can achieve this little bit using "caret", but it will only add text at particular cursor position, i m using kepress event and setinterval (onFocus of textarea) function which will clear extra text from textarea, and clearInterval (onBlur of textarea)

function insertTextAtCaret(el, text) 
{
    var val = el.value, endIndex, range;
    if (typeof el.selectionStart != "undefined" && typeof el.selectionEnd != "undefined") {
        endIndex = el.selectionEnd;
        el.value = val.slice(0, endIndex) + text + val.slice(endIndex);
        el.selectionStart = el.selectionEnd = endIndex + text.length;
    } else if (typeof document.selection != "undefined" && typeof document.selection.createRange != "undefined") {
        el.focus();
        range = document.selection.createRange();
        range.collapse(false);
        range.text = text;
        range.select();
    }
}
SML
  • 2,172
  • 4
  • 30
  • 46
  • 1
    Including line breaks due to line wrapping? – John Dvorak Oct 15 '12 at 10:26
  • yes, i m inserting \n after 15 char in one line, and i need max total 5 lines. – SML Oct 15 '12 at 10:32
  • and you are stuck on how to delete some text given the start index? – John Dvorak Oct 15 '12 at 10:35
  • 1
    i m stuck, where user coninuously press some key and when he release the key the Chars get written in textarea, but it should remove chars after 5th line – SML Oct 15 '12 at 10:41
  • what if you simply `preventDefault` on the `keypress` event (or return false from the handler)? – John Dvorak Oct 15 '12 at 10:44
  • [This documentation](http://help.dottoro.com/ljefwsqm.php#selection) indicates that the `document.selection` object is not supported by any modern browser, only by IE and opera. – John Dvorak Oct 15 '12 at 10:47
  • possible duplicate of http://stackoverflow.com/questions/5794734/how-can-i-lock-the-keyboard-to-limit-the-number-of-characters-in-a-textarea?rq=1 – John Dvorak Oct 15 '12 at 10:49
  • something like this ?? http://jsfiddle.net/9ywFS/1/ – charlietfl Oct 15 '12 at 12:04
  • YES, charlietfl , i need exact same functionality, i tried this logic before, but i m stuck on "onkeyup", how to restrict user to type beyond 5 lines. – SML Oct 15 '12 at 13:02
  • look at this and try to integrate with keypress. I won't be able to work on this for a while http://jsfiddle.net/JCehq/1/ – charlietfl Oct 15 '12 at 13:37

1 Answers1

1
function charCountTextarea(textAreaId,e,limit,lineLen)
{   

    var code = e.charCode || e.keyCode;

    newLines = $("#"+textAreaId).val().split("\n").length;
    var t = $("#"+textAreaId)[0];
    var lineIndex = t.value.substr(0, t.selectionStart).split("\n").length-1;
    console.log('val t :'+$("#"+textAreaId).val()+' line index : '+lineIndex+' new lines '+newLines);

    if(lineIndex == 10 && $("#"+textAreaId).val().split("\n")[lineIndex].length>(lineLen+1) && code!=8 && code!=46 && code!=33 && code!=34 && code!=35 && code!=36 && code!=37 && code!=38 && code!=39 && code!=40)
    {
        $("#"+textAreaId).val(($("#"+textAreaId).val()).substring(0, $("#"+textAreaId).val().length - 1));
        alert('You are reached to limit');
        return false;
    }

    if(lineIndex<5)
    {
        $("#"+textAreaId).val($("#"+textAreaId).val().wordWrap(lineLen, "\n", 0));
    }
    var countLine1 = $("#"+textAreaId).val().split("\n")[0].length;

    if($("#"+textAreaId).val().split("\n")[lineIndex].length>lineLen)  // which will count the total line char condition
    {
        console.log("In condition : ");
        if(code!=8 && code!=46 && code!=33 && code!=34 && code!=35 && code!=36 && code!=37 && code!=38 && code!=39 && code!=40)
        {
            // to insert next line              
            insertTextAtCaret(document.getElementById(textAreaId), "\n");
        }
    }
}
SML
  • 2,172
  • 4
  • 30
  • 46