如何在jquery函数中使用C#bool方法返回值(serverside)(clientside)

时间:2016-03-20 14:43:28

标签: c# jquery methods boolean

这是我的问题,我想在我的代码后面的bool验证方法中获取返回值,并在jquery if语句中使用它...这样的事情 - 通过插入bool方法" validation& #34;进入jquery if语句



  < script >
    $(function() {
      $('form').bind('submit', function() {

        if (Page_IsValid && ***validation()==true***) {
          $('#tableWrapper').fadeTo(3000, 0.0).queue(function(next) {
            $("#tableWrapper").hide();
            next();
          });

        }


      });
    });

  < /script>
&#13;
&#13;
&#13;

这是我的服务器端bool方法 -

 protected bool validate()
{
    if (calExpdate.SelectedDate < DateTime.Now)
    {
        lblExpDateError.Visible = true;
        lblExpDateError2.Visible = true;
        lblExpDateError.ForeColor = System.Drawing.Color.Red;
        lblExpDateError2.ForeColor = System.Drawing.Color.Red;

        lblExpDateError.Text = "\u2022 Expiry Date must be greater then todays date";
        lblExpDateError2.Text = "*";
        return false;
    }

    if (ddlCType.SelectedIndex == 0)

        if (txtCNumber.Text.Length < 15 || txtCNumber.Text.Length > 15)
        {

            lblCardNumError2.Visible = true;
            lblCardNumError.Visible = true;
            lblCardNumError2.ForeColor = System.Drawing.Color.Red;
            lblCardNumError.ForeColor = System.Drawing.Color.Red;
            lblCardNumError2.Text = "*";
            lblCardNumError.Text = "\u2022 Card Number: Please enter 15 digits";
            return false;
        }

    if (ddlCType.SelectedIndex == 1 || ddlCType.SelectedIndex == 2)
    {
        if (txtCNumber.Text.Length < 16 || txtCNumber.Text.Length > 16)
        {

            lblCardNumError.Visible = true;
            lblCardNumError2.Visible = true;
            lblCardNumError2.ForeColor = System.Drawing.Color.Red;
            lblCardNumError.ForeColor = System.Drawing.Color.Red;
            lblCardNumError2.Text = "*";
            lblCardNumError.Text = "\u2022 Card Number: Please enter 16 digits";
            return false;
        }

    }
    return true;
}

这是我的aspx -

&#13;
&#13;
<%@ Page Title="Purchase Form" Language="C#" MasterPageFile="~/Master Pages/FrontEnd.master" AutoEventWireup="true" CodeFile="Purchase.aspx.cs" Inherits="_Purchase" %>

  <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
  </asp:Content>
  <asp:Content ID="Content2" ContentPlaceHolderID="mainCont" runat="Server">
    <div id="fadediv">
      <asp:UpdatePanel runat="server">
        <ContentTemplate>

          <h1>Purchase Information</h1>
          <br />
          <div id="tableWrapper">
            <div runat="server" id="tableWrapper1">
              <table class="auto-style3">
                <tr>
                  <td colspan="3">
                    <h2>Personal and Shipping Information
                                    <br />
                                        Please fill in the form below</h2>
                  </td>
                </tr>
                <tr>
                  <td class="auto-style7">Name</td>
                  <td class="auto-style6">
                    <asp:TextBox ID="txtFName" runat="server" MaxLength="50"></asp:TextBox>
                  </td>
                  <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Text="*" ErrorMessage="Please Enter a First Name" CssClass="ErrorMessage" ControlToValidate="txtFName"></asp:RequiredFieldValidator>
                  </td>
                </tr>


                <tr>
                  <td class="auto-style7">Phone</td>
                  <td class="auto-style6">
                    <asp:TextBox ID="txtPhone" runat="server" TextMode="Number" MaxLength="10"></asp:TextBox>
                  </td>
                  <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please Enter a Phone Number" CssClass="ErrorMessage" ControlToValidate="txtPhone">*</asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="Please enter a phone number between 8 and 10 digits. Area code required" ValidationExpression="^\(?(?:\+?61|0)(?:(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}|4\)?[ -]?(?:(?:[01][ -]?[0-9]|2[ -]?[0-57-9]|3[ -]?[1-9]|4[ -]?[7-9]|5[ -]?[018])[ -]?[0-9]|3[ -]?0[ -]?[0-5])(?:[ -]?[0-9]){5})$"
                    ControlToValidate="txtPhone" CssClass="ErrorMessage">*</asp:RegularExpressionValidator>
                  </td>
                </tr>



                <tr>
                  <td class="auto-style7">Card Type</td>
                  <td class="auto-style6">
                    <asp:DropDownList ID="ddlCType" runat="server" CssClass="ddl">
                      <asp:ListItem Selected="True">AMEX</asp:ListItem>
                      <asp:ListItem>VISA</asp:ListItem>
                      <asp:ListItem>Master Card</asp:ListItem>
                    </asp:DropDownList>
                  </td>
                  <td>&nbsp;</td>
                </tr>
                <tr>
                  <td class="auto-style7">Card Number</td>
                  <td class="auto-style6">
                    <asp:TextBox ID="txtCNumber" runat="server" TextMode="Number"></asp:TextBox>
                  </td>
                  <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ErrorMessage="Please Enter a Card Number" ControlToValidate="txtCNumber" CssClass="ErrorMessage">*</asp:RequiredFieldValidator>
                    <asp:Label ID="lblCardNumError2" runat="server" CssClass="labelcssRed" Visible="False">*</asp:Label>
                  </td>
                </tr>
                <tr>
                  <td class="auto-style7">Expiry Date</td>
                  <td class="auto-style6">
                    <asp:Calendar ID="calExpdate" runat="server" SelectedDate="02/01/2016 22:15:30" VisibleDate="2016-02-01"></asp:Calendar>
                  </td>
                  <td>
                    <asp:Label ID="lblExpDateError2" runat="server" CssClass="labelcssRed" Visible="False">*</asp:Label>
                  </td>
                </tr>
                <tr>
                  <td class="auto-style7">Email</td>
                  <td class="auto-style6">
                    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                  </td>
                  <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="Please Enter an Email" ControlToValidate="txtEmail" CssClass="ErrorMessage">*</asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtEmail" CssClass="ErrorMessage" ErrorMessage="Please enter a valid email address" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
                  </td>
                </tr>
                <tr>
                  <td class="auto-style7">&nbsp;</td>
                  <td class="auto-style6">
                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" CssClass="ErrorMessage" />
                  </td>
                  <td>&nbsp;</td>
                </tr>
                <tr>
                  <td class="auto-style7">&nbsp;</td>
                  <td class="auto-style6">

                    <asp:Label ID="lblCardNumError" runat="server" Visible="False"></asp:Label>
                    <br />

                    <br />
                    <asp:Label ID="lblExpDateError" runat="server" Visible="False"></asp:Label>

                  </td>
                  <td>&nbsp;</td>
                </tr>
                <tr>
                  <td class="auto-style7">&nbsp;</td>
                  <td class="auto-style6">


                    <asp:Button ID="btnSubmit" runat="server" OnClick="Button1_Click" Text="Submit" />&nbsp;
                    <asp:Button ID="btnCancel" runat="server" OnClick="btnCancel_Click" Text="Cancel" CausesValidation="False" />


                  </td>
                  <td>&nbsp;</td>
                </tr>
              </table>
            </div>
          </div>
          <script>
            $(function() {
              $('form').bind('submit', function() {

                if (Page_IsValid) {
                  $('#tableWrapper').fadeTo(3000, 0.0).queue(function(next) {
                    $("#tableWrapper").hide();
                    next();
                  });

                }


              });
            });
          </script>
        </ContentTemplate>
      </asp:UpdatePanel>
    </div>

    <asp:Label ID="message" runat="server" Text="" Style="position: absolute; left: 600px"></asp:Label>
  </asp:Content>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

enter code here发现了回答....在使用jquery之前,尽可能多地进行客户端验证,最难的部分是验证日历控件,但最后我只是在开始日期日历控件是今天的日期,并将日历中的值插入文本框然后验证文本框。