Geeks With Blogs

News



Add to Google

Tim Hibbard CEO for EnGraph software

I get a lot of funny messages sent to me on Where's Tim. I keep them all and put them on this page. Sometimes, people have so much to say that their message exceeds the 160 character message limit for my phone. So I wrote a function that tells people how many characters until the end of the message.

The countChar function takes a textbox object and a div object. The div object will display how many characters are left.

function countChar(txtBox,messageDiv)
    {
        try
        {
            
            count = txtBox.value.length;
            if (count < 160)
            {
                charLeft = 160 - count;
                txt = charLeft + " characters left...keep typing!!";
                messageDiv.innerHTML="<font color=green>" + txt + "</font>";
            }
            else
            {
                messageDiv.innerHTML="<font color=red>Message is too long</font>";
            }
            }
            catch ( e )
            {
                
            }
    }


I can then call that function from the onkeyup event of my textbox like this:

<INPUT type="text" id="txtMessage" 
    onkeyup="countChar(txtMessage,messagesuccess);" 
    onkeydown="if(event.keyCode == 13){document.getElementById('btnSendTextMessage').click();}" >

Posted on Sunday, August 13, 2006 11:15 AM Where's Tim | Back to top


Comments on this post: Counting textbox length with Javascript

# re: Counting textbox length with Javascript
Requesting Gravatar...
ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
fix this, bitch
Left by LoLz on Jun 02, 2007 1:07 PM

# re: Counting textbox length with Javascript
Requesting Gravatar...
gsdfgsdfgdsfgdfgdfgdsfg dfg dfg sdflgkdsfl;gk dfg lfdkg ldfg dflgkdlf kgdflgk fldgk d;lfgk dfgdf gdfs;lgkdflkg dflgkdflgkdfslgk flgksldfkg lkg;lk g;ldfkgldkf g;dflkgdflkg;dflgkdflgkdf;lgkdflg lfdkg;dfl g;lfd
Left by jose on Jul 25, 2007 1:12 PM

# re: Counting textbox length with Javascript
Requesting Gravatar...
Thanks! We used this on Comparati
Left by Rohan on Nov 13, 2007 5:04 PM

# re: Counting textbox length with Javascript
Requesting Gravatar...
Thanku :)_
Left by Gurpreet on Jun 19, 2008 7:29 AM

# re: Counting textbox length with Javascript
Requesting Gravatar...
I cannot get the onkeyup to show on my html page, i am using asp.net microsoft web developer 2008 Express Edition
Left by G on Jun 05, 2009 9:43 AM

# Please Help:!!
Requesting Gravatar...
Can anyone tell me why the F&%*#$ this IF Statement DOESN'T WORK!!! I'm losing my mind and really need the help

//Get length of Text Box
var promolength = document.registration.promocode.value.length;


if (promolength > 0) {

if (promolength !=3 || promolength !=7) {
alert("The Promo Code You Entered Is Invalid... Please Try Again.");
}

}

No matter what I type in it ALWAYS alerts that it's invalid, despite the obvious || OR symbol....
Left by Chad Reitsma on Sep 24, 2009 6:00 AM

# re: Counting textbox length with Javascript
Requesting Gravatar...
Formating of this website is not correct
Left by asdasd on Apr 14, 2010 9:43 AM

# re: Counting textbox length with Javascript
Requesting Gravatar...
Worked like a charm
Left by Manoj on Sep 18, 2014 1:19 PM

Your comment:
 (will show your gravatar)


Copyright © Tim Hibbard | Powered by: GeeksWithBlogs.net