Validate TextBox inside UserControl using JavaScript in ASP.Net

Last Reply on Apr 10, 2013 12:25 AM By Mudassar

Posted on Apr 10, 2013 12:03 AM


I am using a usercontrol which has two textboxes named txtUserName,txtPassword and a button  named btnSubmit.I am using this usercontrol in my webpage.I want to show alert message in the webpage if the user didnot enter the username or password .  

How can i achieve this?



Posted on Apr 10, 2013 12:25 AM

This way.


First Name: <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox><br />
Last Name: <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox><br />


 <uc:UserControl1 ID="UserControl1" runat="server" />
    <asp:Button ID="btnValidate" runat="server" Text="Validate" OnClientClick="return Validate()" />
    <script type="text/javascript">
        function Validate() {
            var txtFirstName = document.getElementById('<%=UserControl1.FindControl("txtFirstName").ClientID %>');
            var txtLastName = document.getElementById('<%=UserControl1.FindControl("txtLastName").ClientID %>');
            if (txtFirstName.value == "") {
                alert("Please enter First Name");
                return false;
            if (txtLastName.value == "") {
                alert("Please enter Last Name");
                return false;