Solved

string size with javascript

Posted on 2002-06-26
8
298 Views
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.");
          document.forms[0].Answer.focus();
          validated=0;
    }

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!!!!
ioanna
0
Comment
Question by:eurodyna
8 Comments
 
LVL 6

Expert Comment

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

<HTML>
<script language="javascript">
   strData = "this is neat";
   alert (strData.length);
</script>
</HTML>
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 7111320
Here's a better example.

<HTML>
<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;
      }
</script>

<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">
</form>
</HTML>
0
 

Author Comment

by:eurodyna
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.
ioanna
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 6

Accepted Solution

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

Expert Comment

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

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

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

  var FIELD_MAXBYTES = 4000;
  function checkMaxLength(sVal)
  {
      var iMaxCharacters = (FIELD_MAXBYTES / getStringBytes(sVal.value))
     if(sVal.value.length>=iMaxCharacters)
     {
          alert("Please don't enter anything else");
     }
  }
  //-->
  </script>
 </head>

 <body>
  <form>
   <input type=text onkeyUp="checkMaxLength(this)">
  </form>
 </body>
</html>
0
 
LVL 1

Expert Comment

by:laax
ID: 7126335
eurodyna,

   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.
Laax.
0
 
LVL 27

Expert Comment

by:BigRat
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.

2.UTF-8
   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.

HTH
0
 

Author Comment

by:eurodyna
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

828 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