how to use textbox1.text value for textbox2's required validator in html

Last post 03-16-2007 10:13 AM by mokeefe. 3 replies.

Sort Posts:

  • how to use textbox1.text value for textbox2's required validator in html

    03-16-2007, 7:33 AM
    • Member
      175 point Member
    • niraj_d418
    • Member since 11-28-2005, 7:22 AM
    • Posts 42

    Hello,

    There a form having two text box, having a required field validator on second textbox, If it's empty i want to display message like, "<field1Name> can't be blank". Where <field1Name> is the value of first textbox on the form (i.e.fldszField1Value.Text)

    How can i do that in <html> </html> section of .aspx file using <% %> ? is it possible to do so ? or will i have to put my code in .aspx.vb file ?
     

    This is a pseudo code of a situation, i do have.

                    <tr>
    <td>
    <asp:TextBox ID="fldszField1Name" runat="server" MaxLength="50"></asp:TextBox>
    </td>
    <td>
    <asp:TextBox ID="fldszField1Value" runat="server" MaxLength="50"></asp:TextBox>
    <asp:RequiredFieldValidator ID="Requiredfieldvalidator1" runat="server" ControlToValidate="fldszField1Name" ErrorMessage= <%= fldszField1Name.Text & "can’t be blank" %> Display="None">Field can’t be blank</asp:RequiredFieldValidator></td>
    </tr>

    regards,
    Niraj sikotara.


    <!-- Search before you scramble. --!>
  • Re: how to use textbox1.text value for textbox2's required validator in html

    03-16-2007, 8:25 AM
    Answer
    • Star
      12,625 point Star
    • docluv
    • Member since 06-29-2002, 11:16 PM
    • Willow Spring NC
    • Posts 1,995
    • TrustedFriends-MVPs

    I think what you really want to do is use a CustomValidator. This validator lets you define both a server-side and client-side (not required) validation method. I use it for exactly these types of cases and it works great. The CustomValidator has a ServerValidate event that you can handle pretty easy in your code-behind, or you can define a method to use declaratively. If you define your own method its signature accepts two methods, source asn Object and args as a ServerValidateEventArgs. The way it works is you simply do your validation routine, in this case making sure the first textbox has an entry when the second textbox is empty. If the form is valid you set the args.IsValid property to true, else it is false. That is it.

    Other than that it works exactly like any other Validator control as far as defining the control to validate, error message, etc. As far as defining your cilent-side validation method, the signature is the same, just do the routine in JavaScript (which I am not very good at so I will not embarass myself :>).

     

    Filed under:
  • Re: how to use textbox1.text value for textbox2's required validator in html

    03-16-2007, 10:12 AM
    Answer
    • Star
      10,830 point Star
    • mokeefe
    • Member since 08-20-2006, 1:15 AM
    • Canberra Australia
    • Posts 2,098

    Here's an example. ensure that you do server side validation also (Server Validate Event of the Custom Validator). Client side validation has no value without it.

    Additionally a TextBox is generally not a good place to report the error as it goes against common UI design guidlines. It should be a label etc and then the value property in the script needs to be changed to innerHTML.

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
        
        <script language="javascript" type="text/javascript">
        
        var elToValidateID = '<%= TextBox1.ClientID %>';
        var elToReportID = '<%= TextBox2.ClientID %>';
        
        function doValidate(sender,args){
            
            args.IsValid = false;
            
            var elToValidate = document.getElementById(elToValidateID);
            var elToReport = document.getElementById(elToReportID);
            
            if(elToValidate){
                     if(elToValidate.value.length < 1){args.IsValid = false;}
                }
            if(!args.IsValid && elToReport){
                elToReport.value = "the error message"; 
            }
        }
        
        </script>
        
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:CustomValidator ClientValidationFunction="doValidate" ControlToValidate="TextBox1" ID="CustomValidator1" runat="server" ErrorMessage="*" ValidateEmptyText="True"></asp:CustomValidator>
            <asp:Button ID="Button1" runat="server" Text="Button" />
        </div>
        </form>
    </body>
    </html>
     

     

    Rgds,
    Martin.

    For the benefit of all users please mark any post answers as appropriate.
  • Re: how to use textbox1.text value for textbox2's required validator in html

    03-16-2007, 10:13 AM
    • Star
      10,830 point Star
    • mokeefe
    • Member since 08-20-2006, 1:15 AM
    • Canberra Australia
    • Posts 2,098

    mokeefe:
        
        function doValidate(sender,args){
           
            args.IsValid = false;
            
          

    Whoops left my test in place. Change the statement to -

    args.IsValid = true;

    Rgds,
    Martin.

    For the benefit of all users please mark any post answers as appropriate.
Page 1 of 1 (4 items)