www.webdeveloper.com
Results 1 to 3 of 3

Thread: Text area bytes limit

Threaded View

  1. #1
    Join Date
    Jan 2010
    Posts
    2

    Text area bytes limit

    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:
    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>
    2 - bytes counter:
    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(/&#37;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>
    Last edited by Kor; 01-11-2010 at 08:50 AM. Reason: wrap the code [code][/code]

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles