Client Side Validation for CustomValidator

Last post 06-27-2007 2:37 AM by Jessica Cao - MSFT. 14 replies.

Sort Posts:

  • Client Side Validation for CustomValidator

    06-25-2007, 1:43 PM

    I am trying to write a client function for a custom validator. I understand that to do a client side function it must be written in javascript.  However, I don't have much experience with Javascript.   The sever side fuction looks like this:

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            int numOfRecords = SalesmanManagerBLL.GetCountARNumber(int.Parse(args.Value));

            if (numOfRecords > 0)
                args.IsValid = false;
            else
                args.IsValid = true;
        }

     What I am confused on is how to call my function (SalesmanManagerBLL.GetCountARNumber () ) in javascript.   The actual function is a member of a c# class I created.  Any suggestions
     

    Fox
  • Re: Client Side Validation for CustomValidator

    06-25-2007, 2:14 PM
    • Loading...
    • robertmazzo
    • Joined on 11-08-2005, 5:31 PM
    • New York, New York
    • Posts 284

     If SalesmanManagerBLL.GetCountARNumber() is pulling the result from a database table, then I believe you cannot do this from client side code (unless you were to perform an asynch callback via javascript using the asp.net ajax toolbox).

    Bob 

     

  • Re: Client Side Validation for CustomValidator

    06-25-2007, 2:22 PM

    Using a custom validator lets you insert your client-side validation method into the asp.net client-side validation "pipeline" so that your custom validation works just like the out-of-the-box asp.net validation.

    Doing client-side validation with the custom validator will not call any server-side (c#) methods, it will instead execute javascript that you will write.  Here's how you do it.

    First, write your client-side javascript, following the same pattern as below:

    <script language="JavaScript">
    <!--
      function ValidateSomething(sender, args)
      {
        var mySomething = args.Value;
    
        if (mySomething != validSomething) 
        {
          args.IsValid = false;
          return;
        }
    
        args.IsValid = true;
      }
    // -->
    </script>

    Either register that script with the Page.ClientScriptManager object, or just simply embed it in your .aspx markup.  Then, you have to set the ClientValidationFunction property of your CustomValidator in your code-behind.  Like this:

    myCustomValidator.ClientValidationFunction = "ValidateSomething";
    myCustomValidator.EnableClientScript = true;

     

    That's it!  Now when your page fires the client-side validation, your custom validation function should be called just like the other O-O-B validators.

    Hope this helps!

    Matt

    Filed under:
  • Re: Client Side Validation for CustomValidator

    06-25-2007, 3:06 PM

     I am trying to use the function SalesmanManagerBLL.GetCountARNumber () to retrieve the number of records from a database with a particular value. If this count is greater than 0, then validator would return false.  v So this function does access the database as robertMazzo mentioned.  Is there any way to do this with javascript?

      

    Fox
  • Re: Client Side Validation for CustomValidator

    06-25-2007, 3:47 PM
    • Loading...
    • jorgandr
    • Joined on 06-28-2004, 4:15 PM
    • Posts 20

    Unfortunately there's no way to do it in JS without calling a web service synchronously and blocking the user's browser.  You'll just have to rely on server side validation. 

  • Re: Client Side Validation for CustomValidator

    06-25-2007, 3:55 PM

    You would have to generate the client-side validation javascript code dynamically.  And basically "hard-code," from the client's perspective the "CountARNumber" in the client-side script.  Does that make sense?

    Try something like this:

     

        string client_side_script = @"
    
          function ValidateSomething(sender, args)
          {
            var mySomething = args.Value;
    
            if (mySomething != " + SalesmanManagerBLL.GetCountARNumber().ToString() + @") 
            {
              args.IsValid = false;
              return;
            }
    
            args.IsValid = true;
          }
        ";
    
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyValidationFunction", client_side_script);
    
     
    Filed under:
  • Re: Client Side Validation for CustomValidator

    06-26-2007, 1:33 AM

    I decided to  try a different approach in which I read the count value from a hidden field .  This hidden field value is compared againt an integer to return true or false.  However, the script doesnt seem to be working.  The script is below:

     

     function PrimeKeyCheck(sender, args)
      {
       var numberOfRecords;
        var hiddenField = document.getElementById("NumOfRecordsHiddenField");
        numberOfRecords = parseInt(hiddenField);
          if (numberOfRecords > 0)
          {
             args.IsValid = false;
             return;
          }

        args.IsValid = true;
        return;

      }

    I am trying  to locate the hidden field NumOfRecordsHiddenField on the form to get the values it stores. I am not sure what I am doing wrong. Any suggestions?

     

    Fox
  • Re: Client Side Validation for CustomValidator

    06-26-2007, 9:02 AM

    The script looks fine, but I don't know if you are properly adding the hidden field to the form.  Why don't you just use the code I gave you?

  • Re: Client Side Validation for CustomValidator

    06-26-2007, 9:58 PM

    Hello,

    You didn't get the value of the HiddenField, it should be   var hiddenField = document.getElementById("NumOfRecordsHiddenField").value;

            function PrimeKeyCheck(sender, args)
      {
       var numberOfRecords;
        var hiddenField = document.getElementById("NumOfRecordsHiddenField").value;
        numberOfRecords = parseInt(hiddenField);
          if (numberOfRecords > 0)
          {
             args.IsValid = false;
             return;
          }

        args.IsValid = true;
        return;

      }

    Jessica

    Jessica Cao
    Sincerely,
    Microsoft Online Community Support


    “Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
  • Re: Client Side Validation for CustomValidator

    06-26-2007, 11:53 PM

     I tried this but doesn't seem to be working. It is as if the script cannot find the NumOfRecordsHiddenField  
    control. I tried to use the alert() to print the value in a dialog, but not occurs. Thanks for the help so far. Any suggestion on what is wrong?

    P.S. The control is located on a content panel for aspx page that used a master page.
    Fox
  • Re: Client Side Validation for CustomValidator

    06-27-2007, 12:02 AM

    hi,

    Here is the simple page and works fine

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DetailsViewInsertMode.aspx.cs" Inherits="DetailsViewInsertMode" EnableEventValidation="false" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
        <script type="text/javascript">
             function PrimeKeyCheck(sender, args)
      {
       var numberOfRecords;
        var hiddenField = document.getElementById("NumOfRecordsHiddenField").value;
        numberOfRecords = parseInt(hiddenField);
          if (numberOfRecords > 0)
          {
             args.IsValid = false;
             return;
          }

        args.IsValid = true;
        return;

      }

        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
                 <asp:HiddenField ID="NumOfRecordsHiddenField" runat="server" />
        </div>
            <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="PrimeKeyCheck"
                ErrorMessage="CustomValidator"></asp:CustomValidator>
        </form>
    </body>
    </html>

    The reason it doesn't work in your page I could think is you have used mater pages , the HiddenField's ID is not NumOfRecordsHiddenField any more, it may be something like ctl00$ContentPlaceHolder1$NumOfRecordsHiddenField, in this case ,you should use

     var hiddenField = document.getElementById("<%=NumOfRecordsHiddenField.ClientID%>").value;

    Hope it helps,

    Jessica


     

    Jessica Cao
    Sincerely,
    Microsoft Online Community Support


    “Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
  • Re: Client Side Validation for CustomValidator

    06-27-2007, 12:10 AM

    Thanks for the quick response, but it still not working.  I tried you suggestion but nothing happens. I will try to get by using just the server side validation for my custom validator control   

    Fox
  • Re: Client Side Validation for CustomValidator

    06-27-2007, 12:33 AM

    Hello,

    Make sure you have set value to HiddenField. It should work

    Jessica

    Jessica Cao
    Sincerely,
    Microsoft Online Community Support


    “Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
  • Re: Client Side Validation for CustomValidator

    06-27-2007, 1:30 AM

    I try again with the following code.  I underlined the changes you suggested I make:

     function PrimeKeyCheck(sender, args)
     {
            var numberOfRecords=0;
            var hiddenField = document.getElementById("<%=NumOfRecordsHiddenField.ClientID%>").value;
     
            numberOfRecords = parseInt(hiddenField.value);
            alert(numberOfRecords.value);
             if (numberOfRecords > 0)
            {
                 args.IsValid = false;
                return;
            }

        args.IsValid = true;
        return;

        return ;
      }

     hink it located the hidden field but is as if the control has no value or there is something wrong with the value.  The value stored in the hidden field is saved from a textbox during the textbox's TextChanged event.  I tested to ensure that the value is being saved to the hidden field (NumOfRecordsHiddenField).  Is it possible that the script is called before the TextChanged event occurs?  Thanks again.

     

    P.S. This script is for a client side validation that I am using with a Custom validator to display a ValidatorCallout.  From what I read the ValidatorCallout only works with a Custom Validator when using a client side validation. 

     

     

     

    Fox
  • Re: Client Side Validation for CustomValidator

    06-27-2007, 2:37 AM
    Answer

    Hi

    You are right, the client side validation function fires before the TextChanged Event. when it fires, the TextBox's Text has not been set to the HiddenField value yet. So the value of the HiddenField is null

    Jessica

    Jessica Cao
    Sincerely,
    Microsoft Online Community Support


    “Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other