How do I do a confirm box and then return true??? Help!

Discussion in 'JavaScript' started by MikeLugar, Mar 2, 2011.

  1. #1
    I have this function here that is run with the Onclick attribute for a html button. How do I insert a confirmation box before the fuction returns true. When the user click "OK" on the confirmation box the script needs to return true. It is a delete confirmation. Thanks!

    function validate()
    {


    if(document.form.user.value=="")
    {
    alert("Please Select the Recipient(s)");
    return false;
    }

    return true;
    }
     
    MikeLugar, Mar 2, 2011 IP
  2. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #2
    Try:

    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <script type="text/javascript">
    var answer = confirm ("Are you sure you want to delete your message?")
    if (answer)
    {
    	alert ("Your message has been deleted.")
    }
    else
    {
    	alert ("Canceled!.")
    }
    </script>
    </body>
    </html>
    
    Code (markup):
     
    Last edited: Mar 2, 2011
    HungryMinds, Mar 2, 2011 IP
    Five-0 likes this.