I have an ASP Classic page with SHIFT_JIS charset. It has a meta tag under the <head> section like this:

<meta http-equiv="Content-Type" content="text/html; charset=shift_jis">

My page has a text box (txtName) that should only allow 200 characters. I have a Javascript function that validates the character length, which is called on the onclick() event of my Submit button.

if(document.frmPage.txtName.value.length > 200)
{
    alert("You have exceeded the maximum length of 200.");
    return false;
}

The problem is, Javascript is not getting the correct length of Japanese character encoded in SHIFT_JIS. For example, the character has a SHIFT_JIS length of 8 characters, but Javascript is only recognizing it as one character, probably because of the Unicode encoding that Javascript uses by default. Some characters like has 2 or 3 characters when in SHIFT_JIS.

If I will only depend on the length provided by Javascript, long Japanese characters would pass the page validation and it will try to save on the database, which will then fail because of the 200 maximum length of the DB column. Is there a way to get the SHIFT_JIS length of the Japanese character using Javascript? Is it possible to convert from Unicode to SHIFT_JIS using Javascript? How?

Thanks for the help!

Dani AI

Generated

In ’s setup (Classic ASP + Shift_JIS + IE) the core issue is that JavaScript’s string.length counts UTF‑16 code units, not bytes in Shift_JIS. The only 100% reliable check is done server‑side: encode the submitted text as Shift_JIS and count the bytes the server will actually store. Client checks can help UX but must be treated as approximate.

A robust Classic ASP (VBScript) helper that encodes to Shift_JIS and returns the actual byte length:

Function GetShiftJISByteLen(s)
  Dim stm, ba, n
  Set stm = Server.CreateObject("ADODB.Stream")
  stm.Type = 2         ' adTypeText
  stm.Charset = "shift_jis"
  stm.Open
  stm.WriteText s
  stm.Position = 0
  stm.Type = 1         ' adTypeBinary
  ba = stm.Read
  If IsArray(ba) Then
    n = UBound(ba) - LBound(ba) + 1
  Else
    n = LenB(ba)
  End If
  stm.Close
  Set stm = Nothing
  GetShiftJISByteLen = n
End Function

Call this before inserting/updating the DB and reject input when the byte count exceeds 200. For immediate feedback, expose a tiny ASP endpoint that returns the byte length and call it with AJAX from the page — that keeps client UI fast while still using the authoritative server encoding.

If you want a quick client‑side estimate (not authoritative), use a conservative heuristic: ASCII and half‑width katakana = 1 byte, most other Japanese characters = 2 bytes. Example:

function estimateSjisBytes(str) {
  var len = 0;
  for (var i = 0; i < str.length; i++) {
    var c = str.charCodeAt(i);
    if (c <= 0x7F) len += 1;
    else if (c >= 0xFF61 && c <= 0xFF9F) len += 1; // half-width katakana
    else len += 2; // assume double-byte in Shift_JIS
  }
  return len;
}

Notes and cautions: Surrogate pairs / emoji and characters outside JIS mappings may not convert to Shift_JIS (they may be replaced or fail), so server validation is mandatory. If possible, switch the DB to Unicode (NVARCHAR / UTF-8) to avoid these encoding pitfalls. Using client-side ActiveX (ADODB.Stream in IE) can measure bytes locally, but it is insecure and often disabled — prefer server conversion.

by the way, I'm using Internet Explorer as my browser.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.