/    Sign up×
Community /Pin to ProfileBookmark

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>

[/code]

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>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorJan 11.2010 — As far as I know, the number of characters in javascript (the length property of a text field or a textarea) has nothing to do with the bytes/character nor with the unicode.

What do you need? A code which will count and limit the number of characters in a textarea? Or a bytes counter?
Copy linkTweet thisAlerts:
@odaierauthorJan 11.2010 — Hi Kor,

Thank you for your reply.

As you can see at the script #2, it counts the bytes lengt. So, in Portuguese, the string "avia&#231;&#227;o" ("avia&#231;&#227;o") has 7 characteres length and 9 bytes length. If I use the script # 1 I can limt the character lenght, that may be different of the bytes lenght

Sory about my english.
×

Success!

Help @odaier spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...