I have a text field to type sms message both in english and chinese language. As I have searched, 1 sms can only have 1120 bits. Each english character is 7 bit so it can be 1120/7 = 160 characters and for chinese each character is 16 bits so that is 1120/16 = 70 characters. I need to use jquery to show the words written and words remaining under the text field. how do i do this?
Asked
Active
Viewed 1,895 times
1
-
so one single message contains text either in Chinese or in English language right? – paraS elixiR Mar 10 '17 at 04:13
-
@paraS elixiR yes, allow the input of either language. – Manas Mar 10 '17 at 04:17
3 Answers
1
have a look at below snippet, this counts total characters and total number of message.
you can change the value of $maxVal
to 160/70
$(document).ready(function () {
var $remaining = $('#charNum'),
$messages = $remaining.prev();
$maxVal = 160;
$('.word-counter').keyup(function(){
var chars = this.value.length,
messages = Math.ceil(chars / $maxVal),
remaining = messages * $maxVal - (chars % (messages * $maxVal) || messages * $maxVal);
$remaining.text(remaining + ' characters remaining');
$messages.text(messages + ' message(s) / ');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea name="" id="SMSMessage" cols="30" rows="2" class="form-ctrl word-counter foo-value"></textarea>
<span class="error-message word-counter">0 message(s) / </span><span id="charNum" class="error-message">160 characters remaining</span>

AG_
- 2,589
- 3
- 20
- 32
1
Characters can be single byte ,double byte, triple byte and so on. So single byte follows in a particular range.Same thing is true for other characters.Based on this I have created following functions that will calculate the size of a string on the basis of memory
function getByteLength(normal_val) {
// Force string type
normal_val = String(normal_val);
var byteLen = 0;
for (var i = 0; i < normal_val.length; i++) {
var c = normal_val.charCodeAt(i);
byteLen += c < (1 << 7) ? 1 :
c < (1 << 11) ? 2 :
c < (1 << 16) ? 3 :
c < (1 << 21) ? 4 :
c < (1 << 26) ? 5 :
c < (1 << 31) ? 6 : Number.NaN;
}
return parseInt(byteLen)*8;
}
I have created a js fiddle that will work for you. http://jsfiddle.net/paraselixir/d83oaa3v/6/

paraS elixiR
- 1,866
- 1
- 18
- 26
-
why is it counting 1 bit for one character? isn't is supposed to be 8-bit for one english character? – Manas Mar 10 '17 at 08:50
-
oh sorry it is counting memory in bytes earlier so I have multiplied the return value by 8 . and updated the jsfiddle as well. – paraS elixiR Mar 10 '17 at 15:25
-
This is plain wrong on so many levels. It returns 8 times the number of bytes that are calculated by some strange algorithm. But SMS uses either a 7-bit encoding or UTF-16. I have written about this here: https://stackoverflow.com/a/76321264/12534 – Christian Davén May 24 '23 at 08:04
0
there is a package that we can use for this issue, checkout: https://github.com/danxexe/sms-counter

Manas
- 3,060
- 4
- 27
- 55