Page 1 of 1

Size in bytes in textarea contents

Posted: Sat Apr 09, 2005 11:06 am
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

Posted: Sat Apr 09, 2005 11:58 am
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.

Posted: Sat Apr 09, 2005 12:10 pm
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.

Posted: Sat Apr 09, 2005 1:34 pm
by timvw
i would restrict on the number of chars in the database...

don't know if you will accept multibyte strnigs etc....

Posted: Sat Apr 09, 2005 1:55 pm
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.

Posted: Sat Apr 09, 2005 1:59 pm
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.

Posted: Sat Apr 09, 2005 3:08 pm
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.

Posted: Sat Apr 09, 2005 4:54 pm
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)