troubleshooting Question

Javascript - Calculting the number of charcters in an SMS

Avatar of NerishaB
NerishaBFlag for South Africa asked on
2 Comments1 Solution322 ViewsLast Modified:

I have a page tha allows the user to send SMS's.  I also have a function that will count the characters that the user has left, out of 160.  When the user reaches "0/160", the message counter displays: "Message 2", ans so on.  Now, all of this works fine, except for the fact that the counter seems to to skip the first character, and starts the count only after the second character is entered.  Can anyone help?
See my code below:
<script language="JavaScript" type="text/javascript">

 function limitText(limitField, limitNum){
        var count = limitField.value.length;
        var messagecount = document.getElementById("messagecount");
        var countdown = document.getElementById("countdown");

        if (count < limitNum){
                countdown.innerHTML = (limitNum - 1) - count;
                messagecount.innerHTML = "Message 1";
        else if (count < limitNum * 2){
                countdown.innerHTML = (limitNum * 2) - count;
                messagecount.innerHTML = "Message 2";
        else if (count < limitNum * 3){
                countdown.innerHTML = (limitNum * 3) - count;
                messagecount.innerHTML = "Message 3";
                else if (count < limitNum * 4){
                countdown.innerHTML = (limitNum * 4) - count;
                messagecount.innerHTML = "Message 4";
        else {
                limitField.value = limitField.value.substring(0, limitNum * 4);

              <p><span id="header"><textarea style="WIDTH: 489px; HEIGHT: 36px" id="MessagesMessageHeader" cols="44" name="{MessageHeader_Name}">{MessageHeader}</textarea>&nbsp;</span><br>
              <textarea style="WIDTH: 488px; HEIGHT: 63px" onkeydown="limitText(this.form.MessagesMessageBody, 161);" id="MessagesMessageBody" rows="4" cols="46" name="{MessageBody_Name}">{MessageBody}</textarea>&nbsp;<br>
              <div id="sms">
                <span style="FONT-WEIGHT: bold" id="countdown">160</span> / 160 characters<br>
                <span id="messagecount">&nbsp;</span> 
              <span id="footer"><textarea style="WIDTH: 489px; HEIGHT: 44px" id="MessagesMessageFooter" rows="1" cols="43" name="{MessageFooter_Name}">{MessageFooter}</textarea></span></td>
Justin Mathews

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros