CapturedTech.com

Captured Technology

Turning On/Off APS.Net Client Side Validation

Mar 06 2009

Turning On/Off APS.Net Client Side Validation

Javascript has a built in function, ValidatorEnable, to turn on or turn off client validation so you can validate certain controls under only certain conditions. The code attached shows how to limit the validation on a textbox depending on value of a dropdown list. The code also only displays the textbox when the same condition is true.

<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
        function Validate()
        {
            if(document.getElementById("DropDownList1").selectedIndex < 2)
                {
        document.getElementById("TextBox1").disabled = true;
        ValidatorEnable(document.getElementById("valrqdOther"), true);
        }
            else
        {
        document.getElementById("TextBox1").disabled = false;
        ValidatorEnable(document.getElementById("valrqdOther"), false);
        }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" >
            <asp:ListItem Text="1"></asp:ListItem>
            <asp:ListItem Text="2"></asp:ListItem>
            <asp:ListItem Text="3"></asp:ListItem>
            <asp:ListItem Text="4"></asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="valrqdOther" runat="server" 
ControlToValidate="TextBox1" ErrorMessage="- TextBox1 is a required field."
Display="Dynamic" InitialValue="" Enabled="false"></asp:RequiredFieldValidator> </div> </form> public partial class SomeUserClass : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DropDownList1.Attributes.Add("onchange", "javascript:return Validate()"); } }

Blog Directory

Latest technology news.
 Patrick Stevens
 554  246480  11/15/2024

FaceBook

Translate