-2

Have a small if/else problem in javascript. i don't see the logic...

The function should check if two dates are present and give alerts if it's not present. If everything is OK, it should say so...

function update_booking() {
    //from_date = The date of arrival
    //to_date = The date of departure

    var alert = ""; //reset alerts

    //get variables from booking form input
    var from_date = new Date(document.getElementById('from').value);
    var to_date = new Date(document.getElementById('to').value);

    //if arrival and departure date is present
    if(from_date && to_date) {
        var alert = "Everything is OK";
    }

    //if one or two dates are missing
    else {
        //if arrival and departure dates are missing
        if(from_date == 'undefined' || to_date == 'undefined'){
            var alert = "Arrival date and departure date are missing";  
        }

        //if from_date is missing update with value from to_date
        if(from_date == 'undefined') {
            var alert = "Arrival date are missing";
        }

        //if to_date is missing update with value from from_date
        if(from_date == 'undefined') {
            var alert = "Departure date are missing";
        }
    } //end else if one or more date(s) missing

    //write alerts
    document.getElementById('alert').innerHTML = alert;
}
  • 4
    Please update your question to tell us what you expect to happen and what really happens. – George Cummins Apr 29 '13 at 18:01
  • By the way, don't define `var alert` anywhere except the top of the function. See [this about var hoisting in Javascript](http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html). – sinelaw Apr 29 '13 at 18:02
  • 1
    Are you confused between `"undefined"` and `undefined`? – Dave Newton Apr 29 '13 at 18:03
  • For one, it should be `== undefined` (without quotes), but I don't think `new Date()` returns undefined if the constructor is not a valid date. – JJJ Apr 29 '13 at 18:03

3 Answers3

3

If the value of Date is invalid, new Date(datestring) returns an object that is Not a Number (NaN).

For example, both new Date("") and new Date(" ") return an object that is NaN, while new Date("04-29-2013") does not.

So, I would recommend changing the search for undefined to NaN using isNaN() as in:

if(isNaN(from_date) || isNaN(to_date)){
KernelPanik
  • 8,169
  • 1
  • 16
  • 14
  • 3
    Good approach. However, `new Date(garbage)` doesn't actually return `NaN`. It does return an object such that `isNaN()` returns `true`, but `typeof(new Date("xyz"))` is `'object'`, while `typeof(NaN)` is `'number'`. – Ted Hopp Apr 29 '13 at 18:19
  • @Ted Hopp Thanks, good catch. I edited the response to clarify. – KernelPanik Apr 29 '13 at 18:25
1

new Date will not return undefined

> new Date("")
Invalid Date

So looking at your code

if(from_date == 'undefined' || to_date == 'undefined'){

It will never go into that if statement.

To check if a date is valid, you want to use isNaN with getTime()

Community
  • 1
  • 1
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

try this:

function update_booking() {
//from_date = The date of arrival
//to_date = The date of departure
//get variables from booking form input
var from_date = document.getElementById('from').value;
var to_date =document.getElementById('to').value;

//if arrival and departure date is present
if(from_date && to_date) {
    alert( "Everything is OK");
}

//if one or two dates are missing
else {
    //if arrival and departure dates are missing
    if(from_date == ""|| to_date == ""){
        alert("Arrival date or departure date are missing");  
    }

    //if from_date is missing update with value from to_date
    if(from_date == "") {
        alert("Arrival date are missing");
    }

    //if to_date is missing update with value from from_date
    if(to_date == "") {
        alert("Departure date are missing");
    }
} //end else if one or more date(s) missing

}

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
E Behrangi
  • 41
  • 1
  • 9