views:

42

answers:

1

I have a simple HTML Form that allows the user to enter some text which is then sent as an SMS/TXT Message via an SMS Gateway. The user enters the message text into a textarea:

<textarea rows="10" cols="40" id="smsbody" validate = "required:true" name="MessageBody"></textarea>

They can enter as little or as much text as they like, however as each SMS is limited to 160 characters I would like to display a character counter that shows both the number of characters entered and then also calculates how many SMS credits this will use. The formula for calculating the credits is based on the total number of credits entered: if a message exceeds 160 characters, it will be split into multiple message parts. Each message part is restricted to 153 characters in length (7 bytes for headers). So a message of 160 characters will be 1 credit, 306 characters will be 2 credits, 459 characters will be 3 credits and so on.

Ideally I would like this to appear in this format:

0 characters, 1 SMS message(s) 200 characters, 2 SMS message(s)

I'm already using jQuery so happy to use a jQuery based solution as well.

Many thanks, Steve

+2  A: 

Something like this?

Try it out: http://jsfiddle.net/kG8U2/2

I think I understand correctly in that for 1 message, it can be 160 characters, but for more than one, the number of messages is characters divided by 153.

HTML

<textarea rows="10" cols="40" id="smsbody" validate = "required:true" name="MessageBody"></textarea>
<div id='message'>
    <span id='char'>0</span> characters,
    <span id='msgs'>0</span> SMS message(s),
    <span id='remg'>160</span> remaining
</div>​​​​

jQuery

var $chars = $('#char');
var $msgs = $('#msgs');
var $remg = $('#remg');

$('#smsbody').keyup(function(evt) {
    var len = this.value.length;
    $chars.text(len);
    if(len <= 160) {
        $msgs.text(1);
        $remg.text(160 - len);
    } else {
        var multi = Math.ceil((len/153)) ;
        $msgs.text(multi);
        $remg.text((multi * 153) - len);
    }
});​

EDIT: Fixed a flaw where it was off by 1 character, and added a remaining counter for fun.

patrick dw
Hi Patrick thanks very much for that I've tweaked it a little but it's working a treat. Much appreciated. Thanks Steve
Steve Kemp
@Steve - You're welcome! :o)
patrick dw
I've been testing this now and it works well. Once quick question: when you start the SMS Message count is at 0 and if you enter a few characters it increments to 1. When you delete the text so the field is empty the SMS message count stays at 1. Can this be reduced back to zero if there are zero characters again?
Steve Kemp
@Steve - Sure. I changed the first line inside the `if()` to `$msgs.text((len `. See it here: http://jsfiddle.net/kG8U2/3/
patrick dw
Thanks Patrick that works a treat.On a slightly related topic do you know if it's possible to submit the number of characters and SMS Messages with the rest of my HTML form elements in the POST? It's a PHP page and I'm updating a database with the POST values and would like to also update the fields in the database that store the number of characters and SMS Messages. I could calculate these again but would like to avoid that if I can pass them with my form as part of the POST. Many thanks,Steve
Steve Kemp
@Steve - I guess I would have some hidden inputs into which you would copy the numbers when you submit. Something like `$('#myForm').submit( function() { $('#char_hidden').val( $('#char').text(); ) });`
patrick dw
Thanks Patrick I'll check it out. I was just testing the counter on Safari v5 and it wasn't responding. I'm using jQuery validation for some of the form fields and they're working fine but the counter isn't responding (works great on IE8 though). Any ideas what's going on there?
Steve Kemp
@Steve - Not sure. I do my testing in Safari 5, and it works for me. Were you testing it in the jsFiddle, or in your code?
patrick dw
The strange thing is it works in jsFiddle but now on my testing server . . . wierd
Steve Kemp
@Steve - Then there's some other issue. Does any jQuery code work on your testing server? For example, if you do `alert( $.fn.jquery );`, what happens?
patrick dw
It's back working again . . not sure what the problem was but the Javascript debugger in Safari helped me find the problematic line. I then deleted it and pasted it in again a few times and got it working again. Some hidden gremlin in there somewhere . . . thanks for all your help once again.
Steve Kemp
@Steve - You're welcome! :o)
patrick dw