Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

counting form length but not spaces 2

Status
Not open for further replies.

WebRic

Technical User
Sep 21, 2004
95
GB
Hi,

I've a character counter set up in my form. It's to do with engravings and I'm therefore not interested in counting the space marks. Once counted I then multiply by a number currently 0.65.

At the moment I have:

Code:
<textarea name="engraving25" id="engraving25" class="inputbox-nowidth" 
onKeyup="document.checkout.showcount25.value=document.checkout.engraving25.value.length;
document.checkout.priceadd25.value=(document.checkout.engraving25.value.length)*0.65;"></textarea>
<label for="showcount25">Characters</label>
<input type="text" name="showcount25" id="showcount25" value="0" class="inputbox-nowidth" size="3" readonly />
<label for="priceadd25">Price Addition</label>
&pound;<input type="text" name="priceadd25" id="priceadd25" value="0" class="inputbox-noshow" size="3" readonly />

My main interest is setting up the counter without couning the space marks... How would I go about doing this?
Thanks,

Richard

ps. some background on my complete aim is:


that it seems quite messy. There are several fields where this is being used, so ideally I'd like to move the javascript to an external file and pass the field names through to a function.
something like countup('fieldname','showcountfield','priceaddfield','0.65')
 
make this change:
(I used a "this" reference to make your code shorter)
Code:
<textarea name="engraving25" id="engraving25" class="inputbox-nowidth"
onKeyup="document.checkout.showcount25.value=[!]this.value.replace(/ /g, '')[/!].length;
document.checkout.priceadd25.value=(document.checkout.engraving25.value.length)*0.65;"></textarea>

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
can i suggest a function? (it's cleaner...).

Code:
function doCount(v) {
    var e = document.forms['checkout'];
    v = v.replace( " ", "" );
    e['showcount25'].value = v.length;
    e['priceadd25'].value = v.length * 0.65;
}

Code:
<textarea name="engraving25" id="engraving25" class="inputbox-nowidth"
onKeyup="doCount(this.value);">



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Thank you both for your help.

Finalised code looks like:

Code:
function doCount(v,s,p) {
    var e = document.forms['checkoutform'];
	v = v.replace(/\s+/g,'');
    e[s].value = v.length;
	n= v.length * 0.65;
    e[p].value = n.toFixed(2) 
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top