Kaleon
Kaleon

Reputation: 53

Return not stopping form submit (javascript)

I'm currently trying to stop a form from submitting through a confirm(), however while it does return false when I step through the code it doesn't seem top stop the code from submitting. I'm referring the the called return in the confirm purchase, is my problem that it is only returning the false to the datevalidate function and not the onsubmit?

function datevalidate() {
var dateNow = new Date();
var month = document.getElementById("expmon").value;
var year = document.getElementById("expyear").value;

var currentDate= new Date(year,month);

if (currentDate < dateNow){
alert("Your card has expired please enter valid details")
return false;
}

confirmpurchase();
}

function confirmpurchase(){
cPurchase = confirm("Are you sure you want to proceed?");
if (cPurchase==false){
return=false;}

}

Upvotes: 0

Views: 116

Answers (2)

Seanna
Seanna

Reputation: 31

I would suggest using event.preventDefault()

function datevalidate(event)
    if (!confirmpurchase) {
        event.preventDefault()
    }

Upvotes: 0

NoGray
NoGray

Reputation: 1149

Your datevalidate() doesn't return anything when it calls the confirm function. You need to return the confirmpurchase() value.

//.....
if (currentDate < dateNow){
        alert("Your card has expired please enter valid details")
        return false;
    }

    return confirmpurchase(); // need to return the confirm value
    // an easier option is return confirm("Are you sure you want to proceed?");
}

Also, your confirmpurchase() needs to return true if valid (e.g. add return true in the end).

Upvotes: 1

Related Questions