Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

string size with javascript

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
eurodyna
Asked:
eurodyna
1 Solution
 
TriskelionCommented:
Yes.  It is called "length".

<HTML>
<script language="javascript">
   strData = "this is neat";
   alert (strData.length);
</script>
</HTML>
0
 
TriskelionCommented:
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
 
eurodynaAuthor Commented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
TriskelionCommented:
When you say greek and spanish, are you talking about just the accented and special character above 127 or others?
0
 
CJ_SCommented:
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
 
laaxCommented:
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
 
BigRatCommented:
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
 
eurodynaAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now