Guest User

Untitled

a guest
Jan 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. function insertTextAtCaret(el, text) {
  2. var val = el.value, endIndex, range;
  3. if (typeof el.selectionStart != "undefined" && typeof el.selectionEnd != "undefined") {
  4. endIndex = el.selectionEnd;
  5. el.value = val.slice(0, endIndex) + text + val.slice(endIndex);
  6. el.selectionStart = el.selectionEnd = endIndex + text.length;
  7. } else if (typeof document.selection != "undefined" && typeof document.selection.createRange != "undefined") {
  8. el.focus();
  9. range = document.selection.createRange();
  10. range.collapse(false);
  11. range.text = text;
  12. range.select();
  13. }
  14. }
  15.  
  16.  
  17.  
  18. function charCounts(e,textAreaId)
  19. {
  20. var controlKeys = [8, 46, 37, 38,39,40];
  21. var isControlKey = controlKeys.join(",").match(new RegExp(e.which));
  22. var code = (e.keyCode ? e.keyCode : e.which);
  23. var Length = $("#"+textAreaId).val().length;
  24.  
  25.  
  26. var textBox = document.getElementById(textAreaId);
  27.  
  28.  
  29. var newLineChar = "n";
  30. //var t = $("#tx1")[0];
  31. //console.log(t.value.substr(0, t.selectionStart).split("n").length);
  32.  
  33. var AmountLeft = 75 - Length;
  34. var t = $("#"+textAreaId)[0];
  35. var lineIndex = t.value.substr(0, t.selectionStart).split("n").length-1;
  36. //console.log("aaa"+stringOccurrences($("#tx1").val(),"n"));
  37. console.log("line index : "+textAreaId+" line index : "+lineIndex+"textarea cha count : "+Length);
  38.  
  39. if(Length<76)
  40. {
  41. if(lineIndex<5)
  42. {
  43. if($("#"+textAreaId).val().split("n")[lineIndex].length>14)
  44. {
  45. insertTextAtCaret(document.getElementById(textAreaId), "n");
  46. }
  47.  
  48. }
  49. else
  50. {
  51. console.log("line in else");
  52. if(isControlKey)
  53. {
  54. //return;
  55. }
  56. else
  57. {
  58. e.preventDefault();
  59. }
  60. }
  61. }
  62. else
  63. {
  64. console.log("76 in else");
  65. if(isControlKey)
  66. {
  67. //return;
  68. }
  69. else
  70. {
  71. e.preventDefault();
  72. }
  73. }
  74.  
  75.  
  76. }
Add Comment
Please, Sign In to add comment