Hi everyone,
I need some suggestion.
Actually i created a text box and wrote a java script to limit its character hold to 500 char. I made my related db column size to 508bytes(Oracle).
The functionality was working fine when i suddenly found a bug.

Say for example if i put 498char as normal char and last 2 as special character("", ~`) my db throws exception as the size is more than 508bytes. The reason behind is the size of special character being not same as normal character.

I thought of two ideas not sure how to proceed!.

Idea1: In my javascript where i check the number of character i will change it to check the total size of all character in bytes and keep it less than or equal to 500.
Problem: Is it really possible to get the byte size of character input in java script?

Idea2: In my java script i will modify the max size based on number of number of special character.
So something like this
MAX_SIZE = 500 - SPECIAL_CHARACTER * SPECIAL CHAR SIZE -1 or something similar.
Problem: Special character dont have fixed size i belive.. So how will i come to know whats the size of which character.

Any other approach are always invited..
Thanks for reading the post!

javascript does provide an escape and unescape methods which might help with this problem.

If you know your encoding of special characters you can also go with Option 2 and make an array of characters looking up length (no small project).

Maybe something like detecting onchange events of your input field and += to the value of a hidden field while escaping that value ... then you could track the size of the total field data with the special characters escaped from the hidden field.

I'm just brain storming here. I don't know if this would actually work or not. But I think it could work. If the character encoding is different than the escaped character encoding you might be able to submit the hidden field to the database ... then unescape the characters after retrieving them from the database.

The only problem that might arise is that the escaped character also might have what is considered a special character '%' and then some digits for encoding, but at least this would be a standard special character which you could account for in your script.

<head>
<script type="text/javascript">
alert( escape( '#' ) );
</script>
</head>

Let me know if I'm not making any sense.

Good luck

This article has been dead for over six months. Start a new discussion instead.