2

Number of characters / sms using jQuery

 2 years ago
source link: https://www.codesd.com/item/number-of-characters-sms-using-jquery.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Number of characters / sms using jQuery

advertisements

I count characters using NobleCount and the following code:

$('#message').NobleCount('#messageInfo',{
            max_chars: getMaxChars(),
            on_negative: function(t_obj, char_area, c_settings, char_rem){

            }
});

I want a feature like counting SMS, if the limit is reached the next 160 chars are for the second sms and so on. I can use parameters like on_update, on_positive, on_negative and block_negative.

I tried something using modulo but it does not work. Any ideas?


Firstly, character counting is very easy. You just need to use the length property on a string. To count the number of SMS messages needed, you'll need to divide by 160 and round up (because 161 characters requires 2 messages). Your code should probably look something like this:

HTML:

<textarea name="message" value="" id="message"></textarea>
<p>
    <span id="remaining">160 characters remaining</span>
    <span id="messages">1 message(s)</span>
</p>

jQuery:

$(document).ready(function(){
    var $remaining = $('#remaining'),
        $messages = $remaining.next();

    $('#message').keyup(function(){
        var chars = this.value.length,
            messages = Math.ceil(chars / 160),
            remaining = messages * 160 - (chars % (messages * 160) || messages * 160);

        $remaining.text(remaining + ' characters remaining');
        $messages.text(messages + ' message(s)');
    });
});

See jsFiddle example.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK