Skip to content Skip to sidebar Skip to footer

International Count Sms Characters

I found Count characters/sms using jQuery, but it does not support international characters such as Chinese, Japanese, Thai, etc. var $remaining = $('#remaining'), $messages =

Solution 1:

You can't really count in "characters" here. According to the SMS article on Wikipedia one of three different encodings are used for SMS (7-bit GSM, 8-bit GSM and UTF-16). So first you'll need to know/decide which encoding you'll be using.

If you know you'll always be using UTF-16, then you can count the number of 16-bit code units a string will take up. A standard SMS can consist of 70 16-bit code units. But this will limit messages in Latin characters to 70, too. So if you want to use the full 160 characters (with 7-bit encoding) or 140 characters (with 8-bit encoding) for Latin characters, then you'll need to distinguish between the three cases.

Example for counting UTF-16 16-bit code units:

var message = "您好,請問你吃飯了嗎?";

var utf16codeUnits = 0;

for (var i = 0, len = message.length; i < len; i++) {
  utf16codeUnits += message.charCodeAt(i) < 0x10000 ? 1 : 2;
}

BTW, this will come up with then same numbers you posted as "incorrect", so you'll need to explain why you consider them incorrect.


EDIT

Despite being accepted already I quickly hacked up a function that correctly (as far as I can say) calculates the GSM 7-bit (if possible) and UTF-16 sizes of a SMS message: http://jsfiddle.net/puKJb/

Post a Comment for "International Count Sms Characters"