validation for empty textbox --

Posted by Sugi under C# on 11/20/2012 | Points: 10 | Views : 1386 | Status : [Member] | Replies : 2
validate one textbox based on other in gridview edit template.if one is empty,other is filled then alert should be

shown..pls provide solution in c# or js..


Posted by: Sandhyab on: 11/20/2012 [Member] Starter | Points: 25


Use Required field validator for this instead of going for c# or JS

<asp:TextBox ID="txtDate" Text='<%#Bind("Date") %>' runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtDate" ErrorMessage="Please enter EventDate">*</asp:RequiredFieldValidator>

Control to validate is Id of the respected textbox to which you want to validate.

Thanks & Regards

Sugi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saratvaddilli on: 11/21/2012 [Member] [MVP] Bronze | Points: 25

hi sugi
here i am providing the code to validate two text-boxes with a condition that two must be filled you just use this code for the text-box of grid view template
In Aspx

<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>
<asp:Button ID="button1" runat="server" OnClick="validate" />

In code behind
 protected void validate(object sender, EventArgs e)

string name1 = TextBox1.Text;
string name2 = TextBox2.Text;
if (((string.IsNullOrEmpty(name1)) && (!string.IsNullOrEmpty(name2))) || ((!string.IsNullOrEmpty(name1)) && (string.IsNullOrEmpty(name2))))
Response.Write("Please fill the two boxes");
Response.Write("Thank You");

Thanks and Regards
Show difficulties that how difficult you are

Sugi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response