• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

Validation Required does not work anymore

My validation does not work anymore after adding the following onclick event to the SUBMIT button:

- onClientClick="return window.confirm('Have you check all of your information for accuracy?');"

It submit the page without asking me to fill-in the required field with the exception of the pop-up window asking for confirmation.  then I got run-error because my required field is empty.  

I have created a regular SUBMIT button and the validation does work.  Can someone tell me what why it does not work?  see code below.

<head runat="server">
<script type="text/javascript">
    function ValidatorEnabled() {
        var checkbox = document.getElementById('<%=CheckBox1.ClientID%>');
        ValidatorEnable(document.getElementById('<%=RequiredFieldValidator1.ClientID%>'), checkbox.checked);
        if (!checkbox.checked) {
            var textbox = document.getElementById('<%=txtTestQty1.ClientID%>');
            textbox.value = '';
        }

        checkbox = document.getElementById('<%=CheckBox2.ClientID%>');
        ValidatorEnable(document.getElementById('<%=RequiredFieldValidator2.ClientID%>'), checkbox.checked);
        if (!checkbox.checked) {
            textbox = document.getElementById('<%=txtTestQty2.ClientID%>');
            textbox.value = '';
        }

        checkbox = document.getElementById('<%=CheckBox3.ClientID%>');
        ValidatorEnable(document.getElementById('<%=RequiredFieldValidator3.ClientID%>'), checkbox.checked);
        if (!checkbox.checked) {
            textbox = document.getElementById('<%=txtTestQty3.ClientID%>');
            textbox.value = '';
        }

        checkbox = document.getElementById('<%=CheckBox4.ClientID%>');
        ValidatorEnable(document.getElementById('<%=RequiredFieldValidator4.ClientID%>'), checkbox.checked);
        if (!checkbox.checked) {
            textbox = document.getElementById('<%=txtTestQty4.ClientID%>');
            textbox.value = '';
        }

       function window.confirm(str) {
        execScript('n = msgbox("' + str + '","4132")', "vbscript");
        return (n == 6);
    }  
</script>    
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    page1 aspx code:

<p>Please fill out items in <span class="style1">RED</span>.&nbsp; Select the tests
            to perform.&nbsp;</p>
        <span class="style1"><b>Today&#39;s Date</b>:</span>
        <asp:TextBox ID="txtfiSubmitDate" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="DateRequiredFieldValidator" runat="server"
            ErrorMessage="Please enter date" ControlToValidate="txtfiSubmitDate">*</asp:RequiredFieldValidator>
        <br />
        <br />
        <span class="style1"><b>Company</b>:</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:TextBox ID="txtfiCompany" runat="server" Width="500px"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator17" runat="server"
            ControlToValidate="txtfiCompany"
            ErrorMessage="Company name is required">*</asp:RequiredFieldValidator>
        <br />
        <span class="style1"><b>Contact Name:</b></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;
        <asp:TextBox ID="txtfiContact" runat="server" Width="200px"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator18" runat="server"
            ErrorMessage="Contact name is required"
            ControlToValidate="txtfiContact">*</asp:RequiredFieldValidator>
        &nbsp;&nbsp;&nbsp; Billing Contact:&nbsp;
        <asp:TextBox ID="txtBillingContact" runat="server"></asp:TextBox>
        <br />
        <span class="style1"><b>Billing Address</b>:</span>&nbsp;&nbsp;&nbsp; <asp:TextBox ID="txtBillingAddress" runat="server"
            Width="500px"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator19" runat="server"
            ErrorMessage="Billing address is required"
            ControlToValidate="txtBillingAddress">*</asp:RequiredFieldValidator>
        <br />
        <b>PO#</b>(if testing is to be charged):
        <asp:TextBox ID="txtPOAddress" runat="server" Width="400px"></asp:TextBox>
        <br />
        <span class="style1"><b>Phone</b>:</span>
        <asp:TextBox ID="txtPhoneNum" runat="server"></asp:TextBox>
&nbsp;<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cell</b>:&nbsp;
        <asp:TextBox ID="txtCell" runat="server"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>Fax</b>:
&nbsp;
        <asp:TextBox ID="txtfiFax" runat="server"></asp:TextBox>
       
        <br />
                <span class="style1"><b>Contact Email</b>:</span>
        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<table class="style1">
            <tr>
                <td class="style2">
                    test description</td>
                <td class="style2">
                    Test Method</td>
                <td>
                    How Many</td>
            </tr>
            <tr>
                <td rowspan="3" class="style2">

<asp:CheckBox ID="CheckBox2" runat="server"
                        Text="Test1" /> <br>              

<asp:CheckBox ID="CheckBox3" runat="server"
                        Text="Test2" /> <br>              

<asp:CheckBox ID="CheckBox4" runat="server"
                        Text="Test3" />                </td>
                <td class="style2">

                    Test 1 Method</td>
                <td rowspan="3">
                    <asp:TextBox ID="txtTestQty1" runat="server" Width="60px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                        ControlToValidate="txtTestQty1" Enabled="False"
                        ErrorMessage="Please Enter Number of Tests">*</asp:RequiredFieldValidator>
                    <br />
                    <asp:TextBox ID="txtTestQty2" runat="server" Width="60px"></asp:TextBox>                
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator20" runat="server"
                        ControlToValidate="txtTestQty2" Enabled="False"
                        ErrorMessage="Please Enter Number of Tests">*</asp:RequiredFieldValidator>
                    <br />
              <asp:TextBox ID="txtTestQty3" runat="server" Width="60px"></asp:TextBox>                
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator21" runat="server"
                        ControlToValidate="txtTestQty3" Enabled="False"
                        ErrorMessage="Please Enter Number of Tests">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Test 2 Method</td>
            </tr>
            <tr>
                <td class="style2">
                    Test 3 Method</td>
            </tr>
</table>
<asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Confirm" Text="Submit" onClientClick="return window.confirm('Have you check all of your information for accuracy?');"> </asp:Button>


page 1 aspx.vb code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

'auto fill form if its the same customer
        If Not String.IsNullOrEmpty(Session("fiSubmitDate")) Then
            txtfiSubmitDate.Text = Session("fiSubmitDate")
        End If

        If Not String.IsNullOrEmpty(Session("fiCompany")) Then
            txtfiCompany.Text = Session("fiCompany")
        End If

        If Not String.IsNullOrEmpty(Session("fiContact")) Then
            txtfiContact.Text = Session("fiContact")
        End If

        If Not String.IsNullOrEmpty(Session("fiBillingContact")) Then
            txtBillingContact.Text = Session("fiBillingContact")
        End If

        If Not String.IsNullOrEmpty(Session("fiAddress")) Then
            txtBillingAddress.Text = Session("fiAddress")
        End If

        If Not String.IsNullOrEmpty(Session("fiPOAddress")) Then
            txtPOAddress.Text = Session("fiPOAddress")
        End If

        If Not String.IsNullOrEmpty(Session("fiPhone")) Then
            txtPhoneNum.Text = Session("fiPhone")
        End If

        If Not String.IsNullOrEmpty(Session("fiCell")) Then
            txtCell.Text = Session("fiCell")
        End If

        If Not String.IsNullOrEmpty(Session("fiFax")) Then
            txtfiFax.Text = Session("fiFax")
        End If

        If Not String.IsNullOrEmpty(Session("fiEmail")) Then
            txtEmail.Text = Session("fiEmail")
        End If

'cleared Test Qty variable
        Session("fiTestQty1") = 0
        Session("fiTestQty2") = 0
        Session("fiTestQty3") = 0

'cleared test method variable
        Session("fiTestMethod1") = ""
        Session("fiTestMethod2") = ""
        Session("fiTestMethod3") = ""
            
'make sure that qty textbox is not empty if checkbox is checked
        CheckBox1.Attributes.Add("onclick", "ValidatorEnabled();")
        CheckBox2.Attributes.Add("onclick", "ValidatorEnabled();")
        CheckBox3.Attributes.Add("onclick", "ValidatorEnabled();")
        CheckBox4.Attributes.Add("onclick", "ValidatorEnabled();")
            
End Sub

  Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        'grab checkbox value
        Dim h_Table As New Hashtable()
        Dim checkBox As CheckBox
        For Each c As Control In Page.Form.Controls
            If c.[GetType]() Is GetType(CheckBox) Then
                checkBox = TryCast(c, CheckBox)
                If checkBox.Checked Then
                    h_Table.Add(checkBox.ID, checkBox.Text)
                End If
            End If
            checkBox = Nothing
        Next
        Session("values") = h_Table
            
'grab session variable
        Session("fiSubmitDate") = txtfiSubmitDate.Text
        Session("fiCompany") = txtfiCompany.Text
        Session("fiContact") = txtfiContact.Text
        Session("fiBillingContact") = txtBillingContact.Text
        Session("fiAddress") = txtBillingAddress.Text
        Session("fiPOAddress") = txtPOAddress.Text
        Session("fiPhone") = txtPhoneNum.Text
        Session("fiCell") = txtCell.Text
        Session("fiFax") = txtfiFax.Text
        Session("fiEmail") = txtEmail.Text
            
'check to see which test is checked
        If CheckBox1.Checked = True Then
            Session("fiTestQty1") = CInt(txtTestQty1.Text)
            Session("fiCost") = Session("fiTestQty1") * 25
            Session("fiTestMethod1") = "Test Method 1"

        Else
            txtTestQty1.Text = 0

        End If

        If CheckBox2.Checked = True Then
            Session("fiTestQty2") = CInt(txtTestQty2.Text)
            Session("fiTestMethod2") = "Test Method 2"
            Session("fiCost2") = Session("fiTestQty2") * 25
        Else
            txtTestQty2.Text = 0
        End If

        If CheckBox3.Checked = True Then
            Session("fiTestQty3") = CInt(txtTestQty3.Text)
            Session("fiTestMethod3") = "Test Method 3"
            Session("fiCost3") = Session("fiTestQty3") * 10
        Else
            txtTestQty3.Text = 0
        End If

        If CheckBox4.Checked = True Then
            Session("fiTestQty4") = CInt(txtTestQty4.Text)
            Session("fiTestMethod4") = "Test Method 4"
            Session("fiCost4") = Session("fiTestQty4") * 5
        Else
            txtTestQty4.Text = 0
        End If            
            
'direct to another page
        Response.Redirect("page2.aspx")            
End Sub    
    </div>
    </form>    
0
aspnewbie09
Asked:
aspnewbie09
  • 5
  • 4
1 Solution
 
codingbeaverCommented:
Let me know if I understand your problem correctly: you would like to ask user for confirmation ONLY after the validation is passed, otherwise, no confirmation is prompted.
0
 
aspnewbie09Author Commented:
no, I want the customer to fill out all required field and still prompting the customer for accuracy of the test they have request.  Is that possible, or is it too much?
0
 
NazoUKCommented:
You've set CausesValidation="False" on your submit button. This will stop the button from triggering validation.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
codingbeaverCommented:
So no matter if the validation is passed or not, user will be prompted for confirmation?
0
 
aspnewbie09Author Commented:
OK.  Thanks.  I change CausesValidation="True" and still submit without prompting me to enter required field.

the button is as follow:
<asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Confirm" Text="Submit" onClientClick="return window.confirm('Have you check all of your information for accuracy.');"> </asp:Button>

However, in teh Properties window for BUtton2 in the BEHAVIOR, CausesValidation is set to TRUE.  

any ideas?
0
 
aspnewbie09Author Commented:
The Validation must pass and still prompt for confirmation.
0
 
codingbeaverCommented:
1. Write a confirmation JavaScript as the attached code
2. Change your Button's OnClientClick to
OnClientClick="return confirmInput()"
function confirmInput()
{
    Page_ClientValidate();
    if (Page_IsValid)
    {
        return confirm('Have you check all of your information for accuracy?');
    }
    else
    {
        return false;
    }
}

Open in new window

0
 
codingbeaverCommented:
Does it work?
0
 
aspnewbie09Author Commented:
yes.
0
 
aspnewbie09Author Commented:
Excellent!  Answers are complete and solutions is complete.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now