...

View Full Version : Extract first numeric string



blkskull
11-01-2006, 07:01 PM
Hi all:
I new here and also new to javascript so I hope you'll be patient if I ask dumb questions. I have a text box with a static value of $2.99 and I want to extract just the 2.99 value and multiply it by a quantity to get a total. While I was searching for something completely different the other day I came across function(I think) which it said would extract the first numeric string in a string including decimal places if there were any. The example it gave was if you had a string of "a62.99retail" it would extract "62.99". The thing is I can't remember what the function was called or where I found it, if anyone could tell me what this function is or a similar one it would greatly appreciated.

Philip M
11-01-2006, 08:17 PM
If the value of $2.99 is static (which I understand to mean unchanging) why do you not simply multiply the quantity by 2.99 to get the total?

blkskull
11-01-2006, 08:35 PM
I have several boxes that have different values(prices) in them which can change so I can't really hard code the value 2.99 in the equation. The way I have to do it is document.form1.qty.value * document.form1.price.value

Beagle
11-02-2006, 08:05 AM
you could use a regular expression:



var price = document.form1.price.value.match(/[0-9.]+/);

Philip M
11-02-2006, 08:21 AM
price = price.replace(/\$/, ''); // remove dollar sign
price = parseFloat(price); // convert string to number
amount = price * qty;

blkskull
11-02-2006, 06:33 PM
Thanks I'll try both.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum