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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
objVal=taObj.value.replace
instead of just
objVal=taObj.value;
which will remove any whitespace from counting (if you only need the space, replace \s with a space