Size in bytes in textarea contents

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Size in bytes in textarea contents

Post by anjanesh »

I have a textarea. I can figure out the number of characters in it as an when a user is typing in it.
I want the know the size of the textarea content in bytes thats going to take up on my server (MySQL db).
I need to know this on the client side before I insert into the db because I have to restrict to a size limit.
What/How will I need to check ?
1 char = 1 byte ?
Thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

From what I know, one character (including whitespace) is one byte.

Try getting the length of the textarea string and then save that as a file and see if tallies up :?

You can check by just saving it (Use php to do this) and then use filesize() to see if it's as you predicted.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

A text file on my PC containing n characters is n bytes.
But this will vary on servers - if its 32-bit or 64-bit.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i would restrict on the number of chars in the database...

don't know if you will accept multibyte strnigs etc....
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Why do you need to know this on the client side first? This seems a poor option if it's important to do this checking since I can disble JS with one click.

Just because the data reaches the server, doesn't necessarily (i can never spell that dammit) mean that you have to insert it.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Server side checking is being done too.
Client side checking is just for displaying to the user how many bytes he is using up so that he doesnt have to be brought back to the form for editing.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Well in which case I'd just go with:

Code: Select all

var size = textarea.value.length + 2; //Or whatever field type you're using
If it's not accurate due to cross OS then the server will have to perform it checks.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

anjanesh wrote:A text file on my PC containing n characters is n bytes.
But this will vary on servers - if its 32-bit or 64-bit.
Actually, a byte (by eight) is 8 bits .

So, it doesn't matter if you have a register of 32bits or 64bits.
A byte remains 8 bits :)

But you are right if you say a character is usually saved in 7 or 8 bits (=1byte) (ascii).

However, nowadays, there are charsets that use more than one byte (multibyte) (eg: UTF-16)
Post Reply