2016-11-17 42 views
0

這裏我的目標是建立一個基本上使一個或另一個文本框爲必需的字段。 I.E.如果一個文本框有一些不爲空的值,則爲空格。或空,我想要提交按鈕繼續。否則,我希望標準紅色錯誤停止從回發提交併啓動插入功能。我試過寫這個,但迄今爲止沒有好處。我能做些什麼來完成這項工作?2個文本框的條件自定義驗證

<script type="text/javascript"> 
         function validateText(sender, args) { 
          if (args.value !== "") { 
           var textBoxB = document.getElementById('TextBox12'); 
           args.IsValid = (TextBox12.value !== ""); 
          } 
          return; 

          /* 
          if (!string.IsNullOrEmpty(TextBox12.Text + TextBox13.Text)) { 
           args.IsValid = true; 
          } 
          else { 
           if (string.IsNullOrEmpty(TextBox12.Text) && !string.IsNullOrEmpty(TextBox13.Text)) { 
            args.IsValid = true; 
           } 
           else if (string.IsNullOrEmpty(TextBox13.Text) && !string.IsNullOrEmpty(TextBox12.Text)) { 
            args.IsValid = true; 
           } 
           else { 
            args.IsValid = false; 
           } 
          } 
          */ 

         } 
        </script> 
        <div> 
         <div class="left"> 
          <asp:Label ID="Label13" runat="server" Text="Advanced Cancellation:"></asp:Label> 
         </div> 
         <div class="right"> 
          <asp:TextBox ID="TextBox12" runat="server"></asp:TextBox> 
          <asp:CustomValidator ID="CustomValidator1" runat="server" 
           ErrorMessage="Required" 
           ValidationGroup='valGroup1' 
           ClientValidationFunction="validateText" 
           OnServerValidate="ServerValidation" 
           ForeColor="Red" 
           ValidateEmptyText="true"> 
          </asp:CustomValidator> 
          <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" 
           ErrorMessage="Advanced Cancellation must be an integer." 
           ControlToValidate="TextBox12" 
           ValidationExpression="^\+?(0|[1-9]\d*)$" 
           ForeColor="Red"> 
          </asp:RegularExpressionValidator> 

         </div> 
        </div> 
        <div> 
         <div class="left"> 
          <asp:Label ID="Label14" runat="server" Text="Action Required (yyyy-MM-dd):"></asp:Label> 
         </div> 
         <div class="right"> 
          <asp:TextBox ID="TextBox13" runat="server"></asp:TextBox> 
          <asp:CustomValidator ID="CustomValidator2" runat="server" 
           ValidationGroup='valGroup1' 
           ValidateEmptyText="true" 
           ErrorMessage="Required" 
           ClientValidationFunction="validateText" 
           OnServerValidate="ServerValidation" 
           ForeColor="Red"> 
          </asp:CustomValidator> 
          <asp:CompareValidator 
           ID="CompareValidator1" runat="server" 
           Type="Date" 
           Operator="DataTypeCheck" 
           ControlToValidate="TextBox13" 
           ErrorMessage="Please enter a valid date." 
           ForeColor="Red"> 
          </asp:CompareValidator> 
         </div> 
        </div> 
protected void ServerValidation(object source, ServerValidateEventArgs args) 
    { 
     if (!string.IsNullOrEmpty(TextBox12.Text)) 
      args.IsValid = !string.IsNullOrEmpty(TextBox13.Text); 

    } 

這就是我到目前爲止。

+0

爲什麼您使用自定義的驗證我的工作?你可以使用必填的字段驗證器 –

+0

@VaibhavBhatia因爲我不需要它們兩個。我想要或者需要。否則,我會使用所需的字段驗證器 – HelixTitan

+0

我已經在你的code.try中做了一些更正,讓我知道它的工作與否 –

回答

1

我有刪除的JavaScript code.Now設計部分是

<div> 
    <div class="left"> 
     <asp:Label ID="Label13" runat="server" Text="Advanced Cancellation:"></asp:Label> 
    </div> 
    <div class="right"> 
     <asp:TextBox ID="TextBox12" runat="server"></asp:TextBox> 
     <asp:CustomValidator ID="CustomValidator1" runat="server" 
      ErrorMessage="Required" 
      ValidationGroup="valGroup1" 
      OnServerValidate="ServerValidation" 
      ForeColor="Red" 
      ValidateEmptyText="true"> 
     </asp:CustomValidator> 
     <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" 
      ErrorMessage="Advanced Cancellation must be an integer." 
      ControlToValidate="TextBox12" 
      ValidationExpression="^\+?(0|[1-9]\d*)$" 
      ForeColor="Red"> 
     </asp:RegularExpressionValidator> 

    </div> 
</div> 
<div> 
    <div class="left"> 
     <asp:Label ID="Label14" runat="server" Text="Action Required (yyyy-MM-dd):"></asp:Label> 
    </div> 
    <div class="right"> 
     <asp:TextBox ID="TextBox13" runat="server"></asp:TextBox> 
     <asp:CustomValidator ID="CustomValidator2" runat="server" 
      ValidationGroup="valGroup1" 
      ValidateEmptyText="true" 
      ErrorMessage="Required" 
      OnServerValidate="ServerValidation" 
      ForeColor="Red"> 
     </asp:CustomValidator> 
     <asp:CompareValidator 
      ID="CompareValidator1" runat="server" 
      Type="Date" 
      Operator="DataTypeCheck" 
      ControlToValidate="TextBox13" 
      ErrorMessage="Please enter a valid date." 
      ForeColor="Red"> 
     </asp:CompareValidator> 
    </div> 
</div> 
    <div> 
     <asp:Button ID="btnok" runat="server" ValidationGroup="valGroup1" /> 
    </div> 

和代碼背後,是

protected void ServerValidation(object source, ServerValidateEventArgs args) 
    { 
     if (!string.IsNullOrEmpty(TextBox12.Text) || !string.IsNullOrEmpty(TextBox13.Text)) 
      args.IsValid = true; 
     else 
     { 
      args.IsValid = false; 
     } 

    } 

我已經測試過它的