locked
preventing server side execution on validation failure RRS feed

  • Question

  • User-640323567 posted

    Hi,

    I have two textboxes with ajax calendar extenders and I am trying to apply validations around those textboxes. Validation seems to be working fine but when validation fails, my server side methods are getting called and throws exceptions..
    Which I don't want.
    How do I stop the server side methods once the validation through javascript fails?
    Here is what I am doing..
    My two textboxes with calendar extenders and validators. I am using 'OnClientDateSelectionChanged' on calendarextender and calling this javascript function.

    <td>Date Range:
                        <asp:TextBox ID="txtDate" runat="server" AutoPostBack="true" OnTextChanged="txtDate_TextChanged1" ValidationGroup="MKE">1/1/2015</asp:TextBox>
                        <ajaxToolkit:CalendarExtender ID="txtDate_CalendarExtender" runat="server" BehaviorID="txtDate_CalendarExtender" TargetControlID="txtDate" OnClientDateSelectionChanged="return checkDate();"></ajaxToolkit:CalendarExtender>
                        <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtDate" Display="Dynamic" ErrorMessage="Invalid Date" Operator="DataTypeCheck" Type="Date">
                        </asp:CompareValidator>
                        <ajaxToolkit:ValidatorCalloutExtender ID="CompareValidator1_ValidatorCalloutExtender"
                            runat="server" Enabled="True" TargetControlID="CompareValidator1">
                        </ajaxToolkit:ValidatorCalloutExtender>
                        &nbsp; To &nbsp;
    						<asp:TextBox ID="txtDate1" runat="server" AutoPostBack="true" OnTextChanged="txtDate1_TextChanged" ValidationGroup="MKE1">12/31/2015</asp:TextBox>
    
                        <ajaxToolkit:CalendarExtender ID="txtDate1_CalendarExtender" runat="server" BehaviorID="txtDate1_CalendarExtender" TargetControlID="txtDate1"></ajaxToolkit:CalendarExtender>
                      <asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txtDate1" Display="Dynamic" ErrorMessage="Invalid Date" Operator="DataTypeCheck" Type="Date">
                        </asp:CompareValidator>
                        <ajaxToolkit:ValidatorCalloutExtender ID="ValidatorCalloutExtender1"
                            runat="server" Enabled="True" TargetControlID="CompareValidator2">
                        </ajaxToolkit:ValidatorCalloutExtender>
                    </td>
    
    <script type="text/javascript">
    
            function checkDate(sender, args) {
                //debugger;
                if (sender._selectedDate > new Date()) {
                    //create a new date var and set it to the
                    //value of the senders selected date
                    var selectedDate = new Date();
                    selectedDate = sender._selectedDate;
                    //create a date var and set it's value to today
                    var todayDate = new Date();
                    var mssge = "";
    
                    if (selectedDate > todayDate) {
                        //set the senders selected date to today
                        sender._selectedDate = todayDate;
                        //set the textbox assigned to the cal-ex to today
                        sender._textbox.set_Value(sender._selectedDate.format(sender._format));
                        //alert the user what we just did and why
                        alert("Warning! - Date Cannot be in the future");
                        return false;
                    }
                }
    
            }
    
        </script>

    Please guide me through this. Thanks for any help.

    Monday, March 21, 2016 3:00 PM

Answers

  • User61956409 posted

    Hi ASPbun,

    You could try to define a variable to determine whether the form fields are valid, then you could check this variable before you submit form.

    <script>
        var isvalidated = false;
    
        function checkDate(sender, args) {
            //debugger;
            if (sender._selectedDate > new Date()) {
                //create a new date var and set it to the
                //value of the senders selected date
                var selectedDate = new Date();
                selectedDate = sender._selectedDate;
                //create a date var and set it's value to today
                var todayDate = new Date();
                var mssge = "";
    
                if (selectedDate > todayDate) {
                    //set the senders selected date to today
                    sender._selectedDate = todayDate;
                    //set the textbox assigned to the cal-ex to today
                    sender._textbox.set_Value(sender._selectedDate.format(sender._format));
                    //alert the user what we just did and why
                    alert("Warning! - Date Cannot be in the future");
    
                    isvalidated = false;
    
                    return false;
                } else {
                    isvalidated = true;
                }
            }
    
        }
    
    
        function myfunction() {
    
            if (isvalidated) {
                return true;
            } else {
                return false;
            }
        }
    </script>
    
    <asp:Button ID="btnok" runat="server" Text="Submit" OnClientClick="return myfunction();" />

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 22, 2016 2:52 AM