Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 28th, 2005, 06:19 AM
Registered User
 
Join Date: Jan 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default hiding isNan message

I have created a restaurant booking system using JavaScript/ASP. The problem I have is in the booking page where I have written IF statements to check that both the txtcustID and txtSeatNo boxes are completed, and then a further ELSE IF statement makes sure that the customer has not tried to book more seats than is available.

This works fine, except I am getting the default isNaN pop up box before my ALERT message kicks in. For example, if the customer leaves the CustID text box blank, then the isNan pops up, clicking OK then brings up the message telling the customer to complete this field.

My question is this. Is it possible to hide the isNan message and go straight to my ALERT?

Here is the code: -

<SCRIPT LANGUAGE=JavaScript>
    function form1_onsubmit()
    {
        var form = document.form1;
        var returnValue = false;
        var seatspassed = (form.txtSeatsLeft.value);
        var seatswanted = (form.txtSeatNo.value);
        var possible = parseInt(seatspassed) - parseInt(seatswanted);

        alert (possible);

        if (form.txtCustID.value == "")
        {
        alert ("Please enter your Customer ID");
        form.txtCustID.focus();
        }
        else if (form.txtSeatNo.value == "")
        {
        alert ("Please enter the number of seats you would like to book");
        form.txtSeatNo.focus();
        }
        else if (possible < 0)
        {
        alert ("There are only " + seatspassed + " seats available. Please try again.");
        form.txtSeatNo.focus();
        }
        else
        {
        returnValue = true;
        }
        return returnValue;
    }
    </SCRIPT>

 
Old February 28th, 2005, 06:41 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You need to test for NaN before you attempt any arithmetic.
I would never use parseInt, especially without the second argument (which base to covert to), for converting numbers like this, use Math.floor. Try this
Code:
alert(parseInt("012");
alert(parseInt("012", 10);
alert(parseInt("012", 8);
alert(Math.floor("012");
So:
Code:
var seatspassed = (form.txtSeatsLeft.value);
var iSeatsPassed = Math.floor(seatspassed);
if (isNaN(iSeatsPassed))
{
  //alert and exit
}
//repeat for seatswanted
--

Joe (Microsoft MVP - XML)
 
Old February 28th, 2005, 08:38 AM
Registered User
 
Join Date: Jan 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Joe,

Funny thing though; The programme works perfectly now I have powered off then on again - probably something to do with running this through IIS. Getting rid of the 'alert(possible)' also helped (I put this in just to check that the variable contained a value).

Still, you're right about testing for NaN before running the IF statement and I'll try your suggestion.

Thanks again.






Similar Threads
Thread Thread Starter Forum Replies Last Post
hiding an image Michel Kanbour ASP.NET 2.0 Basics 2 August 20th, 2007 12:07 PM
Hiding sub headers.... rupen Crystal Reports 6 May 14th, 2007 04:56 AM
Hiding Footer Arsi CSS Cascading Style Sheets 2 April 13th, 2007 06:55 PM
Hiding scrollbars. rupen HTML Code Clinic 1 September 5th, 2005 07:00 AM
hiding in we config vohra_vikas ADO.NET 2 September 28th, 2004 08:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.