vendredi 16 décembre 2016

How to disable OnClientClick if certain if statements are met?

I have an asp.net web app in C# and i'm using Visual Studio. One of my pages has a button and I have inserted the following in the source code for my button OnClientClick="return confirm('Are you sure you wish to apply?');". In the .cs code for the page, I have a large number of if statements that throw up error messages if the button is clicked but not all the fields have been filled out etc.

What I need is a way to add some type of code that I can add to each of my if statements that disables the OnClientClick message as I only want it to appear if all the fields have been correctly filled out. I have tried a few variations but nothing has worked, is there a simple line of C# code that will help me achieve this? I have included the .cs code for my button click. Thanks in advance!

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");
            }           
    }

Aucun commentaire:

Enregistrer un commentaire