Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




.NET/ASP.NET Interview Question -Validate a Controls in ASP.NET page using JavaScript?

Answer: 

Let's see an Simple example to understand.

Assuming that we have a Textbox and Button on the ASP.NET page like below.

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
OnClientClick="return validate()" />


Now, Add the below code snippet on the page source.

<script language="javascript" type="text/javascript">
function ValidateTextBox()    
{
   if(document.getElementById("<%=txtName.ClientID%>").value == "")         
  {                
alert("Name Field Cannot be Empty");
   document.getElementById("<%=txtName.ClientID%>").focus();
    return false;      
   }
    return true;
}
</script>


Once you have completed the above steps, just run the project and see the result.

 

Please click here to see more .NET/ASP.NET interview questions

Regards,

Visit Authors blog for more .NET/ASP.NET interview questions

Share this article   |    Print    |    Article read by 4183 times
Author:
Shivprasad koirala Koirala
I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small E-learning company in India. We are very much active in making training videos , writing books and corporate trainings. Do visit my site http://www.questpond.com for .NET, C# , design pattern , WCF , Silverlight , LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server training and Interview questions and answers
Related Articles:
Related Interview Questions: