Click to See Complete Forum and Search --> : excluding spaces in a textcount function
drgnfli4
05-29-2003, 03:52 PM
I have a function that counts the characters entered into a textbox...how do I exclude the spaces (or any other char.) from being counted?
the function looks like this:
function textCounter(field, countfield, maxlimit){
{countfield.value = field.value.length;}
}
<html><head><title></title>
<script type="text/javascript">
<!--
var len = 0;
function count(t){
len=t.value;
len=len.split(" ").join("").toString();
len=len.length;
return len;
}
//-->
</script></head>
<body>
<form name="f" action="">
<input type="text" name="t"></textarea>
<input type="button" value="Count w/o Spaces" onClick="count(this.form.t);">
</form></body></html>
Charles
05-29-2003, 05:02 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<script type="text/javascript">
<!--
String.prototype.countAllBut = function (s) {return this.match(new RegExp (s.length > 0 ? '[^' + s + ']' : '.', 'g')).length;}
// -->
</script>
<form action="" onsubmit="elements[0].value = elements[0].value.countAllBut('a-z'); return false">
<div>
<textarea cols="40" rows="5">Faquid gidri gugitronis, sce adrin rhevepsius erhabronis. Uphesch me vuf enopla emo stomitum est. Uplot quidig nibrit sti avaner oh scrahiquidatis. Opames, hospi aho pisunore bopopant.</textarea>
<br><input type="submit" value="count">
</div>
</form>