Vijay Kumar Vijay Kumar - 13 days ago 9
jQuery Question

SMS Character Count

How to create n number of sms counts using jquery?

Like this code only 3 sms count. I need N number of sms count using like this same code.
How to do?

$('#smsText').smsArea();


The HTML:

<b id="smsCount"></b> SMS (<b id="smsLength"></b>) Characters left
<textarea id="smsText"></textarea>




(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));


I tried SMS character Counts. But this code only 3 sms counts. I Need n number of sms counts. Like create more than 100 or 200 sms. If any one know means send the query.

Answer

In this code, the author give arrays with max lengths for only 3 sms :

lengths: {
    ascii: [160, 306, 459],
    unicode: [70, 134, 201]
}

According to this thread, it's calculated like this :

  • one normal ASCII sms will have 160 available characters
  • if you pass to 2 sms, a particular header is required for sending 2 sms as 1 message (the UDH, i think). This header requires 7 characters for each sms. So, 2 sms have (160 - 7) + (160 - 7) = 153 + 153 = 306 available characters
  • Same mechanism for unicode, but max length is 70 and UDH takes 3 characters

So you have to develop a function which calculate this max of available characters for a given number of sms, i propose :

function calculateSmsMaxLength (isUnicode, writtenSmsNb) {

                if (isUnicode) {
                    if (writtenSmsNb == 0) {
                        return 70;
                    } else {
                        return (70 * (writtenSmsNb + 1)) - ((writtenSmsNb + 1) * 3);
                    }
                } else {
                    if (writtenSmsNb == 0) {
                        return 160;
                    } else {
                        return (160 * (writtenSmsNb + 1)) - ((writtenSmsNb + 1) * 7);
                    }
                }
            }

isUnicode : true or false, depending on the content of the sms

writtenSmsNb : the number of sms you want to calculate the max available characters for.

Finally, this is the code for... mmmh, lets say 5 max sms :

(function($){
$.fn.smsArea = function(options){

    var e = this,
        cutStrLength = 0,

        s = $.extend({

            cut: true,
            maxSmsNum: 5,
            interval: 400,

            counters: {
                message: $('#smsCount'),
                character: $('#smsLength')
            },

            calculateSmsMaxLength : function (isUnicode, writtenSmsNb) {

                if (isUnicode) {
                    if (writtenSmsNb == 0) {
                        return 70;
                    } else {
                        return (70 * (writtenSmsNb + 1)) - ((writtenSmsNb + 1) * 3);
                    }
                } else {
                    if (writtenSmsNb == 0) {
                        return 160;
                    } else {
                        return (160 * (writtenSmsNb + 1)) - ((writtenSmsNb + 1) * 7);
                    }
                }
            }
        }, 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;
            }

            for(var sCount = 0; sCount < s.maxSmsNum; sCount++){

                smsType = s.calculateSmsMaxLength(isUnicode, sCount);
                cutStrLength = smsType;
                if(smsLength <= smsType){

                    smsCount = sCount + 1;
                    charsLeft = smsType - 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));


//Start
$(function(){
    $('#smsText').smsArea({maxSmsNum:5});
})

Just change the both "maxSmsNum" values to set the max sms you want to allow. For information, the max is 255 in theory, but in reality, it's often 6 to 8 sms max by sending.

Comments