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

 Posted by ArticlesMaint on 4/19/2011 | Category: ASP.NET Interview questions | Views: 2060

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");
    return false;      
    return true;

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


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response