?
Solved

Character counter minus space bar

Posted on 2008-02-08
3
Medium Priority
?
561 Views
Last Modified: 2010-04-21
I have a character counter that has an absolute limit of 280 characters.  I have made it work to that spec, the only problem i have is that i need to exclude the spacebar as being counted as a character.  The user can only have 280 letters, but the spaces shouldnt count as one.  How would i fix it to where the spacebar wont be counted as a character?  Thanks for any help guys!!!
maxL=255;
function taLimit(taObj) {
	if (taObj.value.length==maxL) return false;
	return true;
}
 
function taCount(taObj,Cnt) { 
	objCnt=createObject(Cnt);
	objVal=taObj.value;
	if (objVal.length>maxL) objVal=objVal.substring(0,maxL);
	if (objCnt) objCnt.innerText=maxL-objVal.length;
	return true;
}
function createObject(objId) {
	if (document.getElementById) return document.getElementById(objId);
	else if (document.layers) return eval("document." + objId);
	else if (document.all) return eval("document.all." + objId);
	else return eval("document." + objId);
}
 
html:
<textarea onKeyPress="return taLimit(this)" onKeyUp="return taCount(this,'myCounter')" name="Description" rows=7 wrap="physical" cols=40>

Open in new window

0
Comment
Question by:chuckbeats
3 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 20851224
You can try:
objVal=taObj.value.replace(/\s/, '');
instead of just
objVal=taObj.value;
which will remove any whitespace from counting (if you only need the space, replace \s with a space
0
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 2000 total points
ID: 20851226
Hi,

On the events I would call the replace function when evaluating string length. It may not be syntactically perfect but I'm not in a postition to try it yet but you get the idea.

Cheers,

Lee

maxL=255;
function taLimit(taObj) {
        if (taObj.value.length.replace(/ /g, '')==maxL) return false;
        return true;
}
 
function taCount(taObj,Cnt) { 
        objCnt=createObject(Cnt);
        objVal=taObj.value.replace(/ /g, '');
        if (objVal.length>maxL) objVal=objVal.substring(0,maxL);
        if (objCnt) objCnt.innerText=maxL-objVal.length;
        return true;
}
function createObject(objId) {
        if (document.getElementById) return document.getElementById(objId);
        else if (document.layers) return eval("document." + objId);
        else if (document.all) return eval("document.all." + objId);
        else return eval("document." + objId);
}
 
html:
<textarea onKeyPress="return taLimit(this)" onKeyUp="return taCount(this,'myCounter')" name="Description" rows=7 wrap="physical" cols=40>

Open in new window

0
 

Author Closing Comment

by:chuckbeats
ID: 31429215
The first suggestion was great, unfortunately after i hit the space bar once and then typed and hit the spacebar again, it would count it, but your solution didnt count it ever, thank you guys
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question