CustomValidator not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • user1980
    New Member
    • Dec 2009
    • 112

    CustomValidator not working

    hello there..


    I have a custom validator which validates three text boxes. my code is,
    Code:
    <table>
      <tr>
        <td colspan="3">
          <asp:CustomValidator ID="CustomValidator_test1" runat="server" 
            ErrorMessage="The name, date, and score fields must be filled in for other tests"
            ClientValidationFunction="ClientValidate_test1" ValidateEmptyText="true" 
            ValidationGroup="Submit"
            onservervalidate="CustomValidator_test1_ServerValidate" 
            SetFocusOnError="false" Display="Dynamic"></asp:CustomValidator>
        </td>
      </tr>
      <tr>
        <td style="border:0">
          <asp:TextBox ID="test1" runat="server" Width="100px"></asp:TextBox>
        </td>
        <td style="border:0" >
          <asp:TextBox ID="test1date" runat="server" Width="80px"></asp:TextBox>
        </td>
        <td style="border:0">
          <asp:TextBox ID="test1score" runat="server" Width="80px"></asp:TextBox>
        </td>
      </tr> 
    </table>
    the clientvalidate function is,

    Code:
    function ClientValidate_test1(sender, args) {
      var Valtest1 = document.getElementById("<%= test1.ClientID %>");
      var Valtest1date = document.getElementById("<%= test1date.ClientID %>");
      var Valtest1score = document.getElementById("<%= test1score.ClientID %>");
                
      if ((Valtest1.value == "" && Valtest1date.value == "" && Valtest1score.value != "") ||
          (Valtest1.value == "" && Valtest1date.value != "" && Valtest1score.value == "") || 
          (Valtest1.value == "" && Valtest1date.value != "" && Valtest1score.value != "") || 
          (Valtest1.value != "" && Valtest1date.value == "" && Valtest1score.value == "") || 
          (Valtest1.value != "" && Valtest1date.value == "" && Valtest1score.value != "") || 
          (Valtest1.value != "" && Valtest1date.value != "" && Valtest1score.value == "")) {
            args.IsValid = false;
        } else if((Valtest1.value != "" && Valtest1date.value != "" && Valtest1score.value != "") || 
          (Valtest1.value == "" && Valtest1date.value == "" && Valtest1score.value == "")){
            args.IsValid = true;
        }
      if (Valtest1.value == "")
        document.getElementById("<%= test1.ClientID%>").focus();
      else if (Valtest1date.value == "")
        document.getElementById("<%= test1date.ClientID%>").focus();
      else if (Valtest1score.value == "")
        document.getElementById("<%= test1score.ClientID %>").focus(); 
    }
    the servervalidate function is

    Code:
    protected void CustomValidator_test1_ServerValidate(object source, ServerValidateEventArgs args)
    {
      if ((test1.Text == "" && test1date.Text == "" && test1score.Text != "") || (test1.Text == "" && test1date.Text != "" && test1score.Text == "") || 
        (test1.Text != "" && test1date.Text != "" && test1score.Text != "") || (test1.Text != "" && test1date.Text == "" && test1score.Text == "") || 
        (test1.Text != "" && test1date.Text == "" && test1score.Text != "") || (test1.Text != "" && test1date.Text != "" && test1score.Text == ""))
        {
          args.IsValid = false;
        }
        else if ((test1.Text != "" && test1date.Text != "" && test1score.Text != "") || (test1.Text == "" && test1date.Text == "" && test1score.Text == ""))
        {
          args.IsValid = true;
        }
      }
    now the problem is even when I fill in all the three textboxes.it still gives me the error message to fill in all the boxes and does not submit the page even when all the three boxes are filled.

    can somebody please let me know where I am going wrong

    thank you
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Is the requirement that all textboxes should contain data?
    Then your If statement should be something like
    Code:
    if ((Valtest1.value != "" && Valtest1date.value != "" && Valtest1score.value != ""){
     //the true case
     //the page should submit.
    }else{
      //one, two, or all of the text boxes did not contain data.
      //discover which ones are empty:
    
       if(Valtest1.value == ""){ 
        //ValTest1 is empty
      }
       if(Valtest1date.value == ""){
        //Valtest1date is empty
      }
       if(Valtest1score.value == ""){
        //Valtest1score is empty
      }
    }
    -Frinny

    Comment

    • user1980
      New Member
      • Dec 2009
      • 112

      #3
      thank you for the response..it works now....

      Comment

      Working...