views:

56

answers:

3

Hi. Is there a way to have a confirm dialog box display the value a user typed in a text box on a form? (For example, if the user types 100.00, I'd like the dialog box display a message something like, "Confirm Amount. Click OK if $100.00 is the correct amount.")

A: 

Yes:

var amount = document.getElementById("textbox").value;
confirm("Confirm Amount. Click OK if $" + amount + " is the correct amount.")

EDIT: Here is a working example: http://jsbin.com/inoru/edit

spinon
Thanks for the reply. I must be missing something. The form submits without popping up the confirm dialog box. Here is what I have for the submit button:<input type="submit" value="Continue" onclick="confirm_amount();" />Here is what I have for the script:<script type='text/javascript'> <!-- function confirm_amount() var payment = document.getElementById("amount").value; confirm("Confirm Amount. Click OK if $" + payment + " is the correct amount.") //--></script>
Spockrates
I added an example.
spinon
Thanks, but I already have validation code running on submit. If I use onsubmit for the form, it prevents the form from validating. It looks like I need an onclick event for the submit button.
Spockrates
Ok well you can just modify to make it for the button then. I just used the form as an example. Just move the onsubmit in the form to the submit button and change it to onclick. It will work the same.
spinon
Thanks again. The confirm message works, but when I click Cancel, the form still submits. Here's what I have for the submit button: <input type="submit" value="Continue" alt="Save scheduled payment" title="Save scheduled payment" onclick="ConfirmAmount();" />And here's what I have for the script:<script type='text/javascript'> <!-- function ConfirmAmount(){ var payment = document.getElementById("amount").value; return confirm("Confirm Amount. Click OK if $" + payment + " is the correct amount."); } //--></script>
Spockrates
The form is using the post method: <form action="/PP?PAGE=ADDSCHEDULEDPAYMENT" method="post" name="paymentform" id="paymentform">
Spockrates
you need to have onclick="return ConfirmAmount();"
spinon
U TH' MAN! Thanks, Spinon. You and Justin have been a great help. :)
Spockrates
Glad we got that figured out.
spinon
I'd give you a vote up, but I'm not up to 15, yet. (Just roll your eyes and call me noobie!);D
Spockrates
Don't worry about it.
spinon
+1  A: 

Sure, you can just pass a string value to the dialog:

var str = "my msg";
confirm(str);

So to display your custom message, just get the value of the text box and append it to your message. For example:

var amount = jQuery("#myTextBox").val();
confirm("Click OK if " + amount + " is the correct amount");
Justin Ethier
i gave a normal javascript approach, but i recommend use this one using jQuery +1
jgemedina
Thanks for replying. I must be doing something wrong, as the dialog box does not pop up. Here is the script:<script type='text/javascript'> <!-- function confirm_amount() var payment = jQuery("#amount").val(); confirm("Click OK if " + payment + " is the correct amount"); //--></script>Here is the button:<input type="submit" value="Continue" onclick="confirm_amount();" />
Spockrates
You have your code commented out via `<!--`. Have you verified you are including the jQuery `.js` file? You may also want to double-check that `val` is the appropriate function to call, although off the top of my head I believe that is the one you want.
Justin Ethier
Thanks. A jQuery datepicker on the page is working OK. I'm pointing to this file: <script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>The comment out is to prevent the js code from displaying on the page in older browsers; it does not prevent the code from running.
Spockrates
Hmm...perhaps you should update your question to include the code you are using. That way we can all see the marked-up code clearly, and can suggest what might be the problem.
Justin Ethier
OK, this code does the trick:<input type="submit" value="Continue" onclick="pay_amount = document.paymentform.amount.value; confirm('Click OK to confirm the amount should be $' + pay_amount + ', or click Cancel to change the amount.');" /> The confirm dialog now shows the value from the amount textbox. However, the form still submits even when I click Cancel. Let me know if you have any ideas.
Spockrates
You need to take the return value of `confirm` and only submit the form if they click 'OK'...
Justin Ethier
Thanks, Justin. I'm feeling a little slow, today (didn't get much sleep). Can you spell it out for me and type out an example code?
Spockrates
Here's a nice example: http://www.htmlite.com/JS006.php
Justin Ethier
Thanks, but I'm looking for a way to prevent a form from submitting when the user clicks cancel.
Spockrates
Thanks for your help, Justin.
Spockrates
A: 

You should check the onblur event from the textbox, if the textbox is not empty then show the message, sth like this:

document.getElementById('textboxid').onblur = function(){
    if(this.value.length > 0 )
        showApplicationMessage()
}
jgemedina
Thanks. But I'm not trying to validate the text field. I'm trying to get a dialog box to display the value the user entered in the text field.
Spockrates
You see, I'm trying to idiot-proof the web form. I'd like to ask the user if she is sure she entered the correct amount in the text box by having her click OK, or abort the submission of the form if she clicks Cancel.
Spockrates