...

View Full Version : Resolved Count on returns (gaps) Text counter - Help



CBG
01-11-2012, 04:35 PM
Hi,

I have the below code it works, but there a problem.
If you press return to drop a line it counts that as a letter/number.

Is there a way to fix it?

<html>
<head>
<title></title>
</head>
<body>
<script language="JavaScript">
function CheckCount()
{
var maxChar = 2000000;
if(document.frmMain.txtDetail.value.length > maxChar)
{
return false;
}
else
{
document.frmMain.txtCount.value = document.frmMain.txtDetail.value.length;
}
}
</script>
<body>
<form name="frmMain" method="post">
<textarea name="txtDetail" cols="40" rows="5" id="txtDetail" onKeyUp="return CheckCount()"></textarea>
<br>
<input name="txtCount" type="text" id="txtCount" style="width:65px;" value="0">
</form>
</body>
</html>

xelawho
01-11-2012, 06:09 PM
how about a little workaround?



<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var enters=0;
function CheckCount(e){
if (window.event) {keycode = window.event.keyCode;}
else if (e) {keycode = e.which;}
var maxChar = 2000000;
if(document.frmMain.txtCount.value > maxChar){
return false;
}
else {
if (keycode == 13) {
enters++
}
document.frmMain.txtCount.value = document.frmMain.txtDetail.value.length-enters;
}
}


</script>
<body>
<form name="frmMain" method="post">
<textarea name="txtDetail" cols="40" rows="5" id="txtDetail" onKeyUp="return CheckCount(event)"></textarea>
<br>
<input name="txtCount" type="text" id="txtCount" style="width:65px;" value="0">
</form>
</body>
</html>

CBG
01-11-2012, 08:54 PM
Just tested this in ie9 and it has the same problem, each return is counted.

adaminaudio
01-11-2012, 09:52 PM
hi there, this worked for me... (not fully tested though)

I'd hate to loop through it constantly, there's got to be a more efficient way :(
somebody might be able to do this through regular expressions or something


anyway, here it is:

function CheckCount()
{

var maxChar = 2000000;

var result = ''; //a string to hold the text without special chars
for(var i=0; i<document.frmMain.txtDetail.value.length; i++){

//loop through and find carriage returns or newline characters
if(document.frmMain.txtDetail.value.charAt(i)=='\n' ||
document.frmMain.txtDetail.value.charAt(i)== '\r')
{
//we find one, get outta here!
continue;

}else{
//otherwise append it to our result string
result += document.frmMain.txtDetail.value.charAt(i);
}

}



if(result.length > maxChar)
{
return false;
}
else
{
document.frmMain.txtCount.value = result.length;
}
}

CBG
01-11-2012, 10:04 PM
Thanks adaminaudio that works for me :)

adaminaudio
01-11-2012, 10:07 PM
hey, You're very welcome :)

xelawho
01-12-2012, 03:35 PM
Just tested this in ie9 and it has the same problem, each return is counted.

oh, yeah... I just noticed that IE counts every "return" as two characters

never a dull moment :rolleyes:

adaminaudio's solution is much more practical - here's the regex version, in case you're interested:



function CheckCount() {
var maxChar = 2000000;
if(document.frmMain.txtCount.value > maxChar){
return false;
}
else{
result=document.frmMain.txtDetail.value.replace(/\n|\r/g,"")
document.frmMain.txtCount.value = result.length;
}
}

adaminaudio
01-12-2012, 04:02 PM
great thanks for that regEx solution xelawho, I'll put that in my box!

I just started on regular expressions and I'm still trying to get my head around the syntax....


-Adam



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum