EasyCFM.COM ColdFusion Forums / Coding Help! / Character Count

   Reply to Discussion | New Discussion << previous || next >> 
Posted By Discussion Topic: Character Count -- page: 1 2

book mark this topic Printer-friendly Version  send this discussion to a friend  new posts last

jansolo
08-12-2008 @ 7:51 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Senior Member
Posts: 218
Joined: Mar 2003

I am looking for a dynamic character counter.

There are loads out there, but they all count spaces.

The website that I am doing, is a website for an engraving service and the
client charges per letter - not spaces. They want it so that as the site user
is typing the message they want engraved, the cost of it is displayed,
updating as needed.

I have searched high and low but can't find anything...

megan
08-12-2008 @ 8:38 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 2398
Joined: Jan 2003

Hi - Coldfusion has many many built in functions that can be combined to do all sorts of things - I suggest you check them out here

for this task see len() and replace()

example:

<cfset MyWords = "Please Count Me!">
<cfset MyCount = len(replace(MyWords," ","","ALL"))>
<cfoutput> #MyCount# </cfoutput>

hth ~megan

We have art so that we shall not die of reality ~ Nietzsche

weathermanfsu
08-12-2008 @ 8:42 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Senior Member
Posts: 183
Joined: Apr 2004

Meghan hit the nail on the head with the replace feature.  

jansolo
08-12-2008 @ 8:52 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Senior Member
Posts: 218
Joined: Mar 2003

Hi Guys,

Sorry, I don't think I made it clear.

I have done similar to Meghan already and it counts the letters and
does the math on submit, but I need it doing as they type.

I have come up with this, but it counts spaces as a character!!

<textarea name="message2" wrap="physical" cols="28" rows="5"
onKeyDown="textCounter(document.myForm.message2,document.myForm.remLen2,8)"
onKeyUp="textCounter(document.myForm.message2,document.myForm.remLen2,0)"><
/textarea>
<br>
<input readonly type="text" name="remLen2" size="6" maxlength="4" value="0">
characters left
<br>
<input type="Submit" name="Submit" value="Submit">
<br>
</form>

<SCRIPT LANGUAGE="JavaScript">
function textCounter(field,cntfield,maxlimit) {
if (field.value.length < 8) // if too long...trim it!
cntfield.value = 8.00;
// otherwise, update 'characters left' counter
else
cntfield.value = ((field.value.length - 8) * 0.30) + 8;
}
</script>

This message was edited by jansolo on 8-12-08 @ 8:54 AM

megan
08-12-2008 @ 8:54 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 2398
Joined: Jan 2003

ok then since you are needing a javascript solution and not a coldfusion solution have you tried any javascript forums ??? ~megan

We have art so that we shall not die of reality ~ Nietzsche

jansolo
08-12-2008 @ 8:55 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Senior Member
Posts: 218
Joined: Mar 2003

Tried em all!!!!!!

megan
08-12-2008 @ 9:05 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 2398
Joined: Jan 2003

Ok - I'll try to look around and see if I can find something - I am a good searcher but not good at javascript at all.  But looking at the problem it seems you would need a test for the space char something like

if strCurChar = Chr(32)  then
intTotalChar = intTotalChar - 1
end if

but don't know enough javascript to actually be able to write a real script for you. Frown

If I find something I'll post back

hth ~megan

We have art so that we shall not die of reality ~ Nietzsche

megan
08-18-2008 @ 12:11 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 2398
Joined: Jan 2003

Hi Jansolo -

I put together a couple of scripts I had on hand then easycfm went down - any probably not perfect for your needs but could give you a place to start:

=========================

<html>
<head>

<script language=javascript>
//Edit the counter/limiter value as your wish
var count = "12";   //Example: var count = "12";
function removeSpaces(string) {
     var tstring = "";
     string = ' + string;
     splitstring = string.split(" ");
     for(j = 0; j < splitstring.length; j++)
     tstring += splitstring[j];
     return tstring;
}
function limiter(){
var tex = removeSpaces(document.myform.comment.value);
var len = tex.length;
if(len > count){
        tex = tex.substring(0,count);
     alert('Only ' + count + ' Characters Allowed');
     return false;
}
document.myform.limit.value = count-len;
}

</script>

</head>
<body>

<form name="myform" METHOD=POST>
<textarea name=comment wrap=physical rows=3 cols=40 onkeyup=limiter()></textarea><br>
<script language=javascript>
document.write("<input type=text name=limit size=4 readonly value="+count+">");
</script>
</form>

</body>
</html>

===========================

hth ~megan


We have art so that we shall not die of reality ~ Nietzsche

jansolo
08-19-2008 @ 7:07 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Senior Member
Posts: 218
Joined: Mar 2003

Hi Megan,

Thank you very much for that. I tried to reply to the post an hour or
two after the last post as I managed to cobble together something
from existing scripts, unfortunately the website was down and I
couldn't log in for a few days!!

I will post here and compare yours to see if it is more efficient!!

Thank you so much for your time on this matter though!!

Jan

Webmaster
08-19-2008 @ 9:33 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Administrator
Posts: 4533
Joined: Jan 2002

Sorry about the downtime guys... It's due to someone with too much time on their hands... (some kids somewhere)... if you catch my drift...

I wish people would just grow up! hehe

Pablo Varando
Senior Application Architect
EasyCFM.COM, LLC.

904.483.1457 \\ mobile
webmaster@easycfm.com \\email

PAGE: 1 2
Sponsored By...
iOpenSoft, LLC is a Houston, Texas Advanced Technology Studio Specializing in Web Design, Web Development, iPhone App Development and Android App Development.