Count characters/sms using jQuery

后端 未结 11 1663
醉酒成梦
醉酒成梦 2020-12-08 17:23

I count characters using NobleCount and the following code:

$(\'#message\').NobleCount(\'#messageInfo\',{
            max_chars: getMaxChars(),
            o         


        
11条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 17:52

    I think the following script can produce more accurate calculation of SMS parts:

    //field: a text box that contains the SMS Text
    //cntField: a text box that will contain the remaining count of characters for each part
    //smsCntfield: a text box that will contain the count of parts
    //lang: 0 for English, 2 for Arabic
    //maxLimit: Maximum count of characters to limit the TextBox, (ex: for 5 SMS in Arabic 331, in English 762
    function textCounter(field, cntfield, smsCntfield, lang, maxlimit) {
    
        part1Count = 0;
        part2Count = 0;
        part3Count = 0;
        part4Count = 0;
        part5Count = 0;
        if (lang == 2) {
            // Arabic
            part1Count = 70;
            part2Count = 63;
            part3Count = 66;
            part4Count = 66;
            part5Count = 66;
        } else if (lang == 0) {
            // English
            part1Count = 160;
            part2Count = 145;
            part3Count = 152;
            part4Count = 152;
            part5Count = 152;
        }
    
        smsCount = 0;
        smsCharCnt = 0;
        smsTotalCount = 0;
    
        if (field.value.length <= part1Count) {
            smsCount = 1;
            smsCharCnt = part1Count;
            smsTotalCount = part1Count;
        } else if (field.value.length <= (part1Count + part2Count)) { 
            smsCount = 2;
            smsCharCnt = part2Count;
            smsTotalCount = (part1Count+part2Count);
        } else if (field.value.length <= (part1Count+part2Count+part3Count)) {
            smsCount = 3;
            smsCharCnt = part3Count;
            smsTotalCount = (part1Count+part2Count+part3Count);
        } else if (field.value.length <= (part1Count+part2Count+part3Count+part4Count)) { 
            smsCount = 4;
            smsCharCnt = part4Count;
            smsTotalCount = (part1Count+part2Count+part3Count+part4Count);
        } else if (field.value.length <= (part1Count+part2Count+part3Count+part4Count+part5Count)) { 
            smsCount = 5;
            smsCharCnt = part5Count;
            smsTotalCount = (part1Count+part2Count+part3Count+part4Count+part5Count);
        }
    
        if (field.value.length > maxlimit) {
            // if too long...trim it!
            field.value = field.value.substring(0, maxlimit);
        } else {
            cntfield.value = smsTotalCount - field.value.length;
            smsCntfield.value = smsCount;
        }
    
    }
    

    example use:

    
                                                                                                    
    ( )

提交回复
热议问题