Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more


string size with javascript

Posted on 2002-06-26
Medium Priority
Last Modified: 2012-05-04
hi all,
i use a form to collect some data that then i insert it in an oracle database. one field is varchar2 and its max size is 4000 bytes. i validate the form input with a javascript using

if (document.forms[0].Answer.value.length>4000)
          alert("The reply text must be less than 4000 characters.");

this works fine if the input is english characters. if the user types other characters as greek or spanish even if the length is less than 4000 i get oracle error, propably the 4000 chars are greater than 4000 bytes.
is there any property, like the length property, i can use to find the size of a string in bytes

please help!!!!
Question by:eurodyna
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 7111293
Yes.  It is called "length".

<script language="javascript">
   strData = "this is neat";
   alert (strData.length);

Expert Comment

ID: 7111320
Here's a better example.

<script language="javascript">
   function DoLength(strData)
      if (strData.length > 4)
         alert(strData.length + " is too long");
         return false;
      alert(strData.length + " is OK");
      return true;

<form name=frmOne onsubmit=DoLength(strMyData.value)>
   <input type=text name=strMyData size=20 value="put text here"></input>
   <input type=submit value="push me">

Author Comment

ID: 7112890
dear Triskelion
if you read my question more carefully you will see that i use the "length" which works fine with english characters where 1 char is 1 byte, but with other characters like greek, spanish etc (that have accent) this doesn't work because 1 of these characters is 2 bytes.
i really don't need to count characters i need to count bytes.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

Triskelion earned 400 total points
ID: 7112894
When you say greek and spanish, are you talking about just the accented and special character above 127 or others?
LVL 22

Expert Comment

ID: 7114630
Not a standard method, but I suppose the following would do. Please check it first on a greek machine.

  <script language="javascript">
  var bArr = new Array(256, 512);
  function getStringBytes(sTxt)

       var iLChar = 0;
       var iCode = 0;
            iCode = sTxt.charCodeAt(i)
            if(iCode>iLChar) iLChar = iCode;
            if(iCode < bArr[i]) return i+1;

  var FIELD_MAXBYTES = 4000;
  function checkMaxLength(sVal)
      var iMaxCharacters = (FIELD_MAXBYTES / getStringBytes(sVal.value))
          alert("Please don't enter anything else");

   <input type=text onkeyUp="checkMaxLength(this)">

Expert Comment

ID: 7126335

   Spanish/Greek characters uses Unicode system, in which
each character occupies two bytes.  So, if your Spanish/Greek text is greater than 2000 characters it will
not be inserted into the database.

Hope it helps.
LVL 27

Expert Comment

ID: 7126771
Javascript is Unicode enabled. That is BY DEFINITION all characters in javascript are 16 bits or occupy two bytes. Hence in an HTML page the characters counted by length() is the numbeer of Unicode characters (better said the number of UTF-16 characters).

When the form is sent to the server the characters end up at the server in one of several possible formats, depending upon which server system you are using :-

1. ISO-8898-X variant
   Ie: 8-bit bytes. Any character outside the "set" is usually represented by inverted question. If this were the case you would loose certain characters, eg: in Greek you'd loose Spanish accents, In Spanish (=ISO-8898-1) you'd loose ALL Greek characters.

   This is an 8-bit encoding for Unicode characters. Up to 6 bytes an represent one full 32-bit Unicode character.

I suspect you have UTF-8 characters at the server.

Your problem is then a Javascript routine which returns the UTF-8 length of a given Javascript string. The routine would have to work on the following lines :-

Unicode Value
000000000xxxxxxx    - gives 1 byte
00000xxxxxxxxxxx    - always gives 2 bytes
xxxxxxxxxxxxxxxx    - always gives 3 bytes

where the 0's and x's are bits. So you should modify CJ_S's routine according to my table and you should be OK.


Author Comment

ID: 7132091
even if i did see your comment before solving my problem your thinking is right.
i used the ASCII codes of each character, if the code is between 0 and 127 then the character counts for 1 byte else the character counts for 2 bytes
this worked perfectly, i think it is a very good solution to such problems

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Simple Linear Regression
Introduction to Processes

647 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question