Hi,
I just found the folloing script that limt the chars in a textarea field. As I am working with UNICODE ( I work in Brasil and our language is portugues whith a several special chars), I woukd like to do the same as the script but changing the limit to bytes. I am a javascript begginer, and found an other script that count the bytes in a string, but I didi not know how to implement it in the first script to limit it to yhe byte count,
Thanks in advance for any help.
Here are the scrips:
1 - Chars limit:
2 - bytes counter:Code:<script language="javascript" type="text/javascript"> <!-- function max1(txarea){ total = 50; tam = txarea.value.length; str=""; str=str+tam; Digitado1.innerHTML = str; Restante1.innerHTML = total - str; if (tam > total){ aux = txarea.value; txarea.value = aux.substring(0,total); Digitado1.innerHTML = total Restante1.innerHTML = 0 } } --> </script> <textarea onKeyUp="java script: return max1(this);" rows="10" cols="80"></textarea> <div>Digitado: <span id="Digitado1">0</span></div> <div>Restante: <span id="Restante1"></span></div>
Code:<script language="javascript" type="text/javascript"> // contar bytes function charCount(str) { // counts the number of unicode characters, which may differ from str.length var esc = escape(str); // escape more robust than encodeURI return esc.replace(/%uD[C-F]../g,'') .replace(/%u..../g,'"') .replace(/%../g,"'") .length; } function SevenBitsOrLess(str) { // returns false if str includes a unicode character requiring 8 or more bits var esc = escape(str); if (esc.match(/%u/)) return false; return (!esc.match(/%[89A-F]/)); } // Example //var str0 = "ab$1_^%25$.%D8%B7 %EA%96%A4,%F3%87%BB%81"; //str = decodeURI (str0); //alert (charCount(str)); /* Note that in the example, str0.length => 40 str.length => 15 alert (charCount(str)) => 14 */ </script>


Reply With Quote

Bookmarks