2011-01-29 81 views
0
<script type="text/javascript"> 
    function clientValidation(sender, arguments) 
    { 
     if (arguments.value == "hello world") 
      arguments.isvalid = true; 
     else 
      arguments.isvalid = false; 

     alert(arguments.isvalid); 
    } 
</script> 

<asp:Label ID="lblName" runat="server" Text="Enter Your Name" /> 
<asp:TextBox ID="txtbxName" runat="server" /> 
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="You are Not allowed" Display="None" ClientValidationFunction="clientValidation" ValidationGroup="ValidationSummary1" /> 
<br /> 
<asp:Label ID="lblClass" runat="server" Text="Class" /> 
<asp:TextBox ID="txtClass" runat="server" /> 
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter Clas" ControlToValidate="txtClass" Display="None" ValidationGroup="ValidationSummary1" /> 
<br />    
<asp:ValidationSummary ValidationGroup="ValidationSummary1" ID="ValidationSummary1" runat="server" /> 
<br /> 
<asp:Button ID="Button1" runat="server" Text="Validate" ValidationGroup="ValidationSummary1" /> 
+1

请问一个问题,而不仅仅是邮政编码 – 2011-01-29 14:56:58

+0

@ El Ronnoco:在上面的代码中,总是存在值为false的警告框为什么如此? – Learner 2011-01-29 14:59:23

回答

4

试试这个,

function clientValidation(sender, arguments) 
{ 
    if (arguments.Value == "hello world") 
    arguments.IsValid = true; 
    else 
    arguments.IsValid = false; 
} 

编辑:设置ControlToValidate属性。

<asp:CustomValidator ID="CustomValidator1" runat="server" 
     ErrorMessage="You are Not allowed" 
     ClientValidationFunction="clientValidation" 
     ValidationGroup="ValidationSummary1" 
     ControlToValidate="txtbxName"> 
</asp:CustomValidator>