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


Groups > comp.lang.javascript > #17179

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

From JJ <jaejunks@glegooilma-swapit.com>
Newsgroups comp.lang.javascript
Subject Re: JavaScript Case Project 3-1: The dreaded NaN error
Date 2012-11-11 20:11 +0000
Organization A noiseless patient Spider
Message-ID <XnsA109212F3FF24jaejunksglegooilma@0.0.0.9> (permalink)
References <055e7215-93ba-40a9-81f3-312ea0e4f5e2@googlegroups.com> <aga4cnF1f0vU1@mid.individual.net> <f361ca82-911a-4f52-9132-3f0af5baf3ca@googlegroups.com> <aga76fF248tU1@mid.individual.net> <f4fcb90e-7930-4622-a518-472813d85cab@googlegroups.com>

Show all headers | View raw


dmn2004404@gmail.com wrote:
> 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? 

Make the calculateShipping function to return a boolean value. Returns 
true to allow submit or false to disallow it. i.e.: due to invalid form 
input(s).

Also check for NaN result each and every parseFloat using isNaN.

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