I count characters using NobleCount and the following code:
$(\'#message\').NobleCount(\'#messageInfo\',{
max_chars: getMaxChars(),
o
Here is small plugin for you. It is my first jQuery plugin i give it for free ;) you just need to start it with:
$('#smsText').smsArea();
The HTML:
<b id="smsCount"></b> SMS (<b id="smsLength"></b>) Characters left
<textarea id="smsText"></textarea>
The Javascript (updated 18.8.2014):
(function($){
$.fn.smsArea = function(options){
var
e = this,
cutStrLength = 0,
s = $.extend({
cut: true,
maxSmsNum: 3,
interval: 400,
counters: {
message: $('#smsCount'),
character: $('#smsLength')
},
lengths: {
ascii: [160, 306, 459],
unicode: [70, 134, 201]
}
}, options);
e.keyup(function(){
clearTimeout(this.timeout);
this.timeout = setTimeout(function(){
var
smsType,
smsLength = 0,
smsCount = -1,
charsLeft = 0,
text = e.val(),
isUnicode = false;
for(var charPos = 0; charPos < text.length; charPos++){
switch(text[charPos]){
case "\n":
case "[":
case "]":
case "\\":
case "^":
case "{":
case "}":
case "|":
case "€":
smsLength += 2;
break;
default:
smsLength += 1;
}
if(text.charCodeAt(charPos) > 127 && text[charPos] != "€") isUnicode = true;
}
if(isUnicode){
smsType = s.lengths.unicode;
}else{
smsType = s.lengths.ascii;
}
for(var sCount = 0; sCount < s.maxSmsNum; sCount++){
cutStrLength = smsType[sCount];
if(smsLength <= smsType[sCount]){
smsCount = sCount + 1;
charsLeft = smsType[sCount] - smsLength;
break
}
}
if(s.cut) e.val(text.substring(0, cutStrLength));
smsCount == -1 && (smsCount = s.maxSmsNum, charsLeft = 0);
s.counters.message.html(smsCount);
s.counters.character.html(charsLeft);
}, s.interval)
}).keyup()
}}(jQuery));
DEMO: http://jsfiddle.net/t32h0gj4/1/
NOTE: there are some basic options
$('#smsText').smsArea({cut:false}); //Do not cut the SMS
$('#smsText').smsArea({maxSmsNum:2}); //2 SMS Max
Once the message has been decoded as Craig McQueen stated in a this thread post and you have your actual character count, to count the needed SMS amount the following is enough:
function cntsms(len){ return len<=0? 0: (len>160? Math.ceil(len/153): 1); }
...I like one-row solutions so much.
$(function() {
$('#message').keydown(function() {
var mychars = $('#message').val().length;
var mysms = Math.ceil(mychars / 160);
console.log (mysms);
console.log (mychars +'' + ' characters');
});
});
Every character from the extension table (GSM Extended Alphabet) is represented by two characters the actual maximum length is dynamically calculated as: 160 - k, where k is the number of extended characters used in the message.
You also have to consider the message length is 153 characters when concatenating messages with a 8bit reference number.
Unicode length is 70 characters, while concatenated 63 characters.
Based on the problems here, I try to write better solution. Adopted from several best answer here.
Javascript
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script>
$(document).ready(function(){
part1Count = 160;
part2Count = 145;
part3Count = 152;
$('#message').keyup(function(){
var chars = $(this).val().length;
messages = 0;
remaining = 0;
total = 0;
if (chars <= part1Count) {
messages = 1;
remaining = part1Count - chars;
} else if (chars <= (part1Count + part2Count)) {
messages = 2;
remaining = part1Count + part2Count - chars;
} else if (chars > (part1Count + part2Count)) {
moreM = Math.ceil((chars - part1Count - part2Count) / part3Count) ;
remaining = part1Count + part2Count + (moreM * part3Count) - chars;
messages = 2 + moreM;
}
$('#remaining').text(remaining);
$('#messages').text(messages);
$('#total').text(chars);
if (remaining > 1) $('.cplural').show();
else $('.cplural').hide();
if (messages > 1) $('.mplural').show();
else $('.mplural').hide();
if (chars > 1) $('.tplural').show();
else $('.tplural').hide();
});
$('#message').keyup();
});
</script>
HTML
<textarea name="message" value="" id="message"></textarea>
<div>
<div><span id="remaining">160</span> Character<span class="cplural">s</span> Remaining</div>
<div>Total <span id="messages">1</span> Message<span class="mplural">s</span> <span id="total">0</span> Character<span class="tplural">s</span></div>
</div>
You can get the code here http://www.mindrestingplace.com/2012/07/18/sms-character-counter/
Hope this will solve your problems. Thanks
And so on...
function cntsms(len) {
return len <= 0 ? 0 : ( len > 160 ? Math.ceil((len-7)/153) : 1);
}