Link to home
Start Free TrialLog in
Avatar of chuckbeats
chuckbeats

asked on

Character counter minus space bar

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

Avatar of hernst42
hernst42
Flag of Germany image

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
ASKER CERTIFIED SOLUTION
Avatar of Lee
Lee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chuckbeats
chuckbeats

ASKER

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