Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
- }
- function charCounts(e,textAreaId)
- {
- var controlKeys = [8, 46, 37, 38,39,40];
- var isControlKey = controlKeys.join(",").match(new RegExp(e.which));
- var code = (e.keyCode ? e.keyCode : e.which);
- var Length = $("#"+textAreaId).val().length;
- var textBox = document.getElementById(textAreaId);
- var newLineChar = "n";
- //var t = $("#tx1")[0];
- //console.log(t.value.substr(0, t.selectionStart).split("n").length);
- var AmountLeft = 75 - Length;
- var t = $("#"+textAreaId)[0];
- var lineIndex = t.value.substr(0, t.selectionStart).split("n").length-1;
- //console.log("aaa"+stringOccurrences($("#tx1").val(),"n"));
- console.log("line index : "+textAreaId+" line index : "+lineIndex+"textarea cha count : "+Length);
- if(Length<76)
- {
- if(lineIndex<5)
- {
- if($("#"+textAreaId).val().split("n")[lineIndex].length>14)
- {
- insertTextAtCaret(document.getElementById(textAreaId), "n");
- }
- }
- else
- {
- console.log("line in else");
- if(isControlKey)
- {
- //return;
- }
- else
- {
- e.preventDefault();
- }
- }
- }
- else
- {
- console.log("76 in else");
- if(isControlKey)
- {
- //return;
- }
- else
- {
- e.preventDefault();
- }
- }
- }
Add Comment
Please, Sign In to add comment