Focus On Textbox if Click on Validation Message

kgovindarao523-21772
Posted by kgovindarao523-21772 under ASP.NET category on | Points: 40 | Views : 2698
Hi,

My Objective is:

While validating the Form, I am displaying validation/Error messages in Validation Summary .
Now if i click on error message i want focus on respective control(text box) .

For example,
form1 contains Personal information and i got error message like 'Name Must be Alphabets only', in validation summary.
if click on this message,the focus must be on 'Name' text box field.

Here is my code.

<form id="form1" runat="server">
<div>
<asp:ValidationSummary ID="vsPersonalProfile" HeaderText="Personal Profile" runat="server"
ValidationGroup="vgPersonalProfile" ForeColor="Red" />
<fieldset>
<legend>Personal Profile</legend>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Name <i style="color: Red">*</i>
</td>
<td>
<asp:TextBox ID="txtName" runat="server" ValidationGroup="vgPersonalProfile"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="reqTxtName" runat="server" ErrorMessage="<a style='cursor:pointer' onclick='javascript:form1.txtName.focus()'>Name must not be empty</a>"
Display="None" ControlToValidate="txtName" ValidationGroup="vgPersonalProfile"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Email <i style="color: Red">*</i>
</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="reqTxtEmail" runat="server" ErrorMessage="<a style='cursor:pointer' onclick='javascript:form1.txtEmail.focus()'>Email must not be empty</a>"
Display="None" ControlToValidate="txtEmail" ValidationGroup="vgPersonalProfile"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Phone Number
</td>
<td>
<asp:TextBox ID="txtPhone" Style="border-color: Red" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
</table>
</fieldset>
<center>
<input type="submit" name="Submit" value="Submit" />
<input type="reset" name="Reset" value="Reset" />
</center>
</div>
</form>


Thank you.

Comments or Responses

Login to post response