I have an asp.net web app and one of my pages is for payment. I have a submit button that will only work if 1) you have selected a currency in which you wish to pay (done with radio buttons) and 2) all of the payment fields have been filled out (card & billing info). This all works great, the last thing missing is if a currency has been selected and all payment info is filled out, I wanted a confirmation box with 'OK' and 'Cancel' buttons. I added the following code into my page_load method: submitPayBtn.OnClientClick = "return confirm('Are you sure you wish to change the name?');";. The issue is that the message box appears whenever I click the submit button, ignoring my if statements already in place.
Is there a way to enable/disable the OnClientClick where applicable within my if statements, so that the box won't appear if a currency hasn't been selected and/or not all of the payment information has been filled out? Cheers!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Coursework
{
public partial class Payment : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
submitPayBtn.OnClientClick = "return confirm('Are you sure you wish to change the name?');";
if (!Page.IsPostBack)
{
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, "PopupScript"))
{
String cstext = "alert('Details are correct, welcome');";
cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
}
}
}
protected void submitPayBtn_Click(object sender, EventArgs e)
{
if (!poundRadBtn.Checked && !usdolRadBtn.Checked && !ozdolRadBtn.Checked && !eurRadBtn.Checked)
if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text == "" || cardBox2.Text == "" || cardBox3.Text == "" || cardBox4.Text == "" || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
{
Response.Write("<script>alert('Please select a currency in which you wish to pay');</script>");
}
if (poundRadBtn.Checked)
if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text.Length < 4 || cardBox2.Text.Length < 4 || cardBox3.Text.Length < 4 || cardBox4.Text.Length < 4 || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry (including a 16 digit card number)');</script>");
}
else if (cardList.SelectedIndex > -1 && cardNameBox.Text.Length > 0 && cardBox1.Text.Length > 3 && cardBox2.Text.Length > 3 && cardBox3.Text.Length > 3 && cardBox4.Text.Length > 3 && expMonList.SelectedIndex > -1 && expYrList.SelectedIndex > -1 && billNameBox.Text.Length > 0 && billAdd1Box.Text.Length > 0 && billAdd2Box.Text.Length > 0 && billCtyBox.Text.Length > 0 && billPostBox.Text.Length > 0 && billCntryBox.Text.Length > 0)
{
Response.Write("<script>alert('Working gbp');</script>");
Response.Redirect("Home.aspx");
}
if (usdolRadBtn.Checked)
if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text.Length < 4 || cardBox2.Text.Length < 4 || cardBox3.Text.Length < 4 || cardBox4.Text.Length < 4 || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry (including a 16 digit card number)');</script>");
}
else if (cardList.SelectedIndex > -1 && cardNameBox.Text.Length > 0 && cardBox1.Text.Length > 3 && cardBox2.Text.Length > 3 && cardBox3.Text.Length > 3 && cardBox4.Text.Length > 3 && expMonList.SelectedIndex > -1 && expYrList.SelectedIndex > -1 && billNameBox.Text.Length > 0 && billAdd1Box.Text.Length > 0 && billAdd2Box.Text.Length > 0 && billCtyBox.Text.Length > 0 && billPostBox.Text.Length > 0 && billCntryBox.Text.Length > 0)
{
Response.Write("<script>alert('Working usd');</script>");
Response.Redirect("Home.aspx");
}
if (ozdolRadBtn.Checked)
if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text.Length < 4 || cardBox2.Text.Length < 4 || cardBox3.Text.Length < 4 || cardBox4.Text.Length < 4 || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry (including a 16 digit card number)');</script>");
}
else if (cardList.SelectedIndex > -1 && cardNameBox.Text.Length > 0 && cardBox1.Text.Length > 3 && cardBox2.Text.Length > 3 && cardBox3.Text.Length > 3 && cardBox4.Text.Length > 3 && expMonList.SelectedIndex > -1 && expYrList.SelectedIndex > -1 && billNameBox.Text.Length > 0 && billAdd1Box.Text.Length > 0 && billAdd2Box.Text.Length > 0 && billCtyBox.Text.Length > 0 && billPostBox.Text.Length > 0 && billCntryBox.Text.Length > 0)
{
Response.Write("<script>alert('Working aud');</script>");
Response.Redirect("Home.aspx");
}
if (eurRadBtn.Checked)
if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text.Length < 4 || cardBox2.Text.Length < 4 || cardBox3.Text.Length < 4 || cardBox4.Text.Length < 4 || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry (including a 16 digit card number)');</script>");
}
else if (cardList.SelectedIndex > -1 && cardNameBox.Text.Length > 0 && cardBox1.Text.Length > 3 && cardBox2.Text.Length > 3 && cardBox3.Text.Length > 3 && cardBox4.Text.Length > 3 && expMonList.SelectedIndex > -1 && expYrList.SelectedIndex > -1 && billNameBox.Text.Length > 0 && billAdd1Box.Text.Length > 0 && billAdd2Box.Text.Length > 0 && billCtyBox.Text.Length > 0 && billPostBox.Text.Length > 0 && billCntryBox.Text.Length > 0)
{
Response.Write("<script>alert('Working eur');</script>");
Response.Redirect("Home.aspx");
}
}
protected void backBtn_Click(object sender, EventArgs e)
{
Response.Redirect("Login.aspx");
}
}
}
You should move the
return confirm('Are you sure you wish to change the name?');
to a separate javascript function. Then you can change it based on other form values.And I suggest you take a look at the build-in aspnet Validation Controls