$(document).ready(function(){ /* If you want to use this script, please keep the original author in this header! Purpose: Script for applying maxlengths to textareas and monitoring their character lengths. Author: James O'Cull Date: 08/14/08 To use, simply apply a maxlenth value to a textarea. If you need it to prevent typing past a certain point, add lengthcut="true" Example: If you add a new text area with javascript, simply call parseCharCounts() again find the new textarea(s) and label them! */ var LabelCounter = 1; function parseCharCounts() { //Get Everything... var elements = document.getElementsByTagName('textarea'); var element = null; var maxlength = 9; var newlabel = null; for(var i=0; i < elements.length; i++) { element = elements[i]; if(element.getAttribute('maxlength') != null && element.getAttribute('limiterid') == null) { maxlength = element.getAttribute('maxlength'); //Create new label newlabel = document.createElement('div'); newlabel.id = 'limitlbl_' + LabelCounter; newlabel.style.color = 'red'; newlabel.style.display = 'inline'; //Make it block so it sits nicely. newlabel.innerHTML = "Updating..."; //Attach limiter to our textarea element.setAttribute('limiterid', newlabel.id); element.onkeyup = function(){ //alert('hello 15'); displayCharCounts(this);}; //Append element //$(this).append(newlabel); element.parentNode.appendChild(newlabel); //Force the update now! displayCharCounts(element); } //Push up the number LabelCounter++; } } function displayCharCounts(element) { var limitLabel = document.getElementById(element.getAttribute('limiterid')); var maxlength = element.getAttribute('maxlength'); var enforceLength = false; if(element.getAttribute('lengthcut') != null && element.getAttribute('lengthcut').toLowerCase() == 'true') { enforceLength = true; } //Replace \r\n with \n then replace \n with \r\n //Can't replace \n with \r\n directly because \r\n will be come \r\r\n //We do this because different browsers and servers handle new lines differently. //Internet Explorer and Opera say a new line is \r\n //Firefox and Safari say a new line is just a \n //ASP.NET seems to convert any plain \n characters to \r\n, which leads to counting issues var value = element.value.replace(/\u000d\u000a/g,'\u000a').replace(/\u000a/g,'\u000d\u000a'); var currentLength = value.length; var remaining = 0; if(maxlength == null || limitLabel == null) { return false; } remaining = maxlength - currentLength; if(remaining >= 0) { //alert('hello'); $('#sunmit_btn_'+LabelCounter).attr("disabled", false); limitLabel.style.color = 'green'; var remaining1 = "
"+ remaining +" chars left
"; limitLabel.innerHTML = remaining1; if(remaining != 1) limitLabel.innerHTML += ''; limitLabel.innerHTML += ''; } else { //alert('hello1'); $('#sunmit_btn_'+LabelCounter).attr("disabled", false); if (enforceLength == true) { value = value.substring(0, maxlength); element.value = value; element.setSelectionRange(maxlength, maxlength); limitLabel.style.color = 'green'; limitLabel.innerHTML = '0'; } else { //Non-negative //alert('hello2'); remaining = Math.abs(remaining); $('#sunmit_btn_'+LabelCounter).attr("disabled", true); limitLabel.style.color = 'red'; var remaining2 = "
- "+ remaining +" chars over
"; limitLabel.innerHTML = remaining2; if (remaining != 1) limitLabel.innerHTML += ''; limitLabel.innerHTML += ''; } } //attr("disabled", true); } //Go find our textareas with maxlengths and handle them when we load! parseCharCounts(); //alert(repliesSet); $(".status_textarea").keyup(function() { //alert('hello 12'); $(this).val().replace(/\u000d\u000a/g,'\u000a').replace(/\u000a/g,'\u000d\u000a'); var value1 = $(this).val(); var countId = $(this).parents('li').find('.count').val(); var currentLength = value1.length; //alert('working'); if (currentLength <= 140) { //alert('1 '+currentLength); $('#sunmit_btn_'+countId).attr("disabled",""); }else{ //alert('2 '+currentLength); $('#sunmit_btn_'+countId).attr("disabled","disabled"); } }); return false; });