Solved

string size with javascript

Posted on 2002-06-26
8
292 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 6

Accepted Solution

by:
Triskelion earned 100 total points
Comment Utility
When you say greek and spanish, are you talking about just the accented and special character above 127 or others?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 22

Expert Comment

by:CJ_S
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
zeroFront challenge 7 70
powerN  challenge 3 46
wordmultiple challenge 12 87
C# code editing and collaboration 3 38
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now