Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #17174

Re: JavaScript Case Project 3-1: The dreaded NaN error

Newsgroups comp.lang.javascript
Date 2012-11-11 10:52 -0800
References <055e7215-93ba-40a9-81f3-312ea0e4f5e2@googlegroups.com> <aga4cnF1f0vU1@mid.individual.net> <f361ca82-911a-4f52-9132-3f0af5baf3ca@googlegroups.com> <aga76fF248tU1@mid.individual.net>
Message-ID <f4fcb90e-7930-4622-a518-472813d85cab@googlegroups.com> (permalink)
Subject Re: JavaScript Case Project 3-1: The dreaded NaN error
From dmn2004404@gmail.com

Show all headers | View raw


On Sunday, November 11, 2012 11:56:33 AM UTC-6, Mel Smith wrote:
> <dmn2004404@gmail.com> wrote in message 
> 
> news:f361ca82-911a-4f52-9132-3f0af5baf3ca@googlegroups.com...
> 
> > On Sunday, November 11, 2012 11:08:42 AM UTC-6, Mel Smith wrote:
> 
> >> where I'm going wrong?
> 
> 
> 
> > Sadly, I think I tried that, and it also failed.
> 
> 
> 
> 
> 
> Hi:
> 
> 
> 
>     Well, *I* tested it on my machine, after inputting 123.45, and got a 
> 
> nice long floating point result !!
> 
> 
> 
>     Remember the *value* of an input field is *text* -- and is *not* a 
> 
> numeric !
> 
> 
> 
>     You must get this 'value' and convert it to a numeric by some means. 
> 
> For example use parseFloat("123.45",10) . This says: Take the string 
> 
> "123.45" and convert it to a floating point numeric with a base 10  ---  
> 
> which is most surely what you wish.
> 
> 
> 
> So, take another 'head-crashing' look at it.
> 
> 
> 
> -Mel Smith
> 
> Mesa, Arizona

Wait -- WAIT! I think I got it -- almost. I've had to make a few changes, as follows. Here's my revised JavaScript:

<script type="text/javascript">
var shipping, total;
function calculateShipping(purchasePrice) {
    if (purchasePrice <= 25)
        shipping = 1.50;
    else
        shipping = purchasePrice * .10;
    total = purchasePrice + shipping;
	window.alert('Purchase price plus shipping and handling is $' + total);
}
</script>

and the revised input tag for the button:

<input type="submit" name="btnCalculate" id="btnCalculate" value="Calculate" onclick="calculateShipping(parseFloat(txtPurchasePrice.value,10))" />

I believe the key was actually to put the value of the text box INSIDE the calculateShipping() function as a parameter.

Anyway, with these revisions, I tried the 123.45 value you attempted, and I got the LONG floating value of 135.79500000000002 -- which looked a little messy. So I attached the "toFixed(2)" method to "totalCost" at the end of the "window.alert" command, and it worked.

But now, I'd like to validate user box input so that if the user should enter a zero, a negative number, or something that actually IS "NaN" (e.g. "Hello world!"), the page will produce a dialog box that says the value is invalid. How might I go about doing that?

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 08:28 -0800
  Re: JavaScript Case Project 3-1: The dreaded NaN error "Mel Smith" <med_cutout_syntel@aol.com> - 2012-11-11 10:08 -0700
    Re: JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 09:34 -0800
      Re: JavaScript Case Project 3-1: The dreaded NaN error "Mel Smith" <med_cutout_syntel@aol.com> - 2012-11-11 10:56 -0700
        Re: JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 10:07 -0800
        Re: JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 10:52 -0800
          Re: JavaScript Case Project 3-1: The dreaded NaN error JJ <jaejunks@glegooilma-swapit.com> - 2012-11-11 20:15 +0000
            Re: JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 12:45 -0800
          Re: JavaScript Case Project 3-1: The dreaded NaN error JJ <jaejunks@glegooilma-swapit.com> - 2012-11-11 20:11 +0000
        Re: JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 12:15 -0800
          Re: JavaScript Case Project 3-1: The dreaded NaN error Osmo Saarikumpu <osmo@weppipakki.com> - 2012-11-12 10:10 +0200
  Re: JavaScript Case Project 3-1: The dreaded NaN error Gregor Kofler <usenet@gregorkofler.com> - 2012-11-12 09:51 +0100
    Re: JavaScript Case Project 3-1: The dreaded NaN error Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-12 11:49 +0100

csiph-web