2011-11-10 55 views
0
<asp:TextBox ID="TextBox3" runat="server" Width="127px"></asp:TextBox> 
<asp:CustomValidator ID="CustomValidator1" runat="server" 
       ControlToValidate="TextBox3" 
       ErrorMessage="Must Be 8 characters" 
       onservervalidate="CustomValidator1_ServerValidate"  
       ValidateEmptyText="True" 
       ValidationGroup="g1">*</asp:CustomValidator> 
    <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="LogIn" 
       ValidationGroup="g1" Width="83px" /> 


protected void CustomValidator1_ServerValidate(object source, 
         ServerValidateEventArgs args) 
    { 
     if (args.Value.Length == 8) 
     { 
      args.IsValid = true; 
     } 
     else 
      args.IsValid = false; 
    } 

我想文本框取8 characters.But該事件不會被炒魷魚up.I haved採取必填驗證以及ValidateEmptyText爲true,但它們中的嘗試不起作用 請幫助。自定義的驗證是不是在asp.net工作

回答

0

嘗試爲TextBox添加RequiredFieldValidator,並且它應該觸發自定義驗證器服務器驗證事件。

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
    ControlToValidate="TextBox3" 
    ErrorMessage="Required"> 
</asp:RequiredFieldValidator> 
+0

我一直在使用必須填寫驗證也試了一下,它不工作 – coder25