0

我的问题是,当我修改密码的值时,我有一个输入密码,它有一个 custumValidator 我检查密码是否不包含名称。当我还编辑字段名称时,我需要触发相同的 customvalidator(客户端验证):我该怎么做:

     <form id="Form1" runat="server">

      <h3>CustomValidator ServerValidate Example</h3>

      <asp:Label id="Message"  
           Text="Enter an even number:" 
           Font-Name="Verdana" 
           Font-Size="10pt" 
           runat="server"/>

      <p>

      <asp:TextBox id="name" 
           runat="server" />
<asp:TextBox id="password" 
           runat="server" />

      &nbsp;&nbsp;

      <asp:CustomValidator id="CustomValidator1"
           ControlToValidate="password"
           ClientValidationFunction="ClientValidate"
           OnServerValidate="ServerValidation"
           Display="Static"
           ErrorMessage="Not an even number!"
           ForeColor="green"
           Font-Name="verdana" 
           Font-Size="10pt"
           runat="server"/>

      <p>

      <asp:Button id="Button1"
           Text="Validate" 
           OnClick="ValidateBtn_OnClick" 
           runat="server"/>

   </form>
</body>
</html>

<script language="javascript"> 
   function ClientValidate(source, arguments)
   {
        // here i will implement :if source contains name
            arguments.IsValid = true;
         else {
            arguments.IsValid = false;
        }
   }
</script>
4

1 回答 1

0

尝试添加这个:

编辑

<form id="Form1" runat="server">
  <script type="text/javascript">
    function ValidatePassword (source, arguments){
      // Gets the name field
      var nameField = document.getElementById('<%= name.ClientId %>');
      // using indexOf since contains does not exist in js.
      // If it is not found it will return -1
      arguments.IsValid = (arguments.Value.indexOf(nameField.value) == -1);
    }
  </script>
  <asp:TextBox id="name" 
       runat="server" />
  <asp:TextBox id="password" 
       runat="server" />

  <asp:CustomValidator id="CustomValidator1"
       ControlToValidate="password"
       ClientValidationFunction="ValidatePassword"
       ErrorMessage="Password should not contain your user name!"
       runat="server"/>

  <asp:Button id="Button1"
       Text="Validate" 
       OnClick="ValidateBtn_OnClick" 
       runat="server"/>

</form>
于 2013-08-01T22:24:07.463 回答