ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Validate empty TextBox on ASP.Net CustomValidator ServerValidate event using C# and VB.Net<p>Hi <a class="username" rel="irshad1231"> irshad1231</a>,</p> <p>Since ControlToValidate is assigned as TextBox, CustomValidator will only fire when the TextBox is not empty.</p> <p>So the work around for this is to set CustomValidator ValidateEmptyText property to true.</p> <p>Check this example. Now please take its reference and correct your code.</p> <p>For demonstration i have used TextBox instead of Label.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;asp:TextBox runat=&#34;server&#34; ID=&#34;lbl_iscitizen&#34; Text=&#34;true&#34; /&gt;&lt;br /&gt; &lt;%--&lt;asp:Label ID=&#34;lbl_iscitizen&#34; runat=&#34;server&#34; Text=&#34;true&#34;&gt;&lt;/asp:Label&gt;--%&gt; &lt;asp:TextBox ID=&#34;lbl_iqamaexpiry&#34; runat=&#34;server&#34; Text=&#34;&#34; CssClass=&#34;textboxAslabel&#34;&gt;&lt;/asp:TextBox&gt; &lt;asp:CustomValidator ID=&#34;cv_name&#34; runat=&#34;server&#34; ControlToValidate=&#34;lbl_iqamaexpiry&#34; Text=&#34;Required&#34; ValidateEmptyText=&#34;true&#34; OnServerValidate=&#34;cv_name_ServerValidate&#34; ValidationGroup=&#34;validate&#34; ForeColor=&#34;Red&#34;&gt;&lt;/asp:CustomValidator&gt;&lt;br /&gt; &lt;asp:Button ID=&#34;submitButton&#34; runat=&#34;server&#34; OnClick=&#34;submitButton_Click&#34; Text=&#34;Submit&#34; ValidationGroup=&#34;validate&#34; /&gt;</pre> <p><strong><span style="text-decoration: underline;">C#</span></strong></p> <pre class="brush: csharp">protected void cv_name_ServerValidate(object source, ServerValidateEventArgs args) { &nbsp;&nbsp;&nbsp; if (lbl_iscitizen.Text.ToLower() == "true") &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; args.IsValid = true; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; args.IsValid = false; &nbsp;&nbsp;&nbsp; } } protected void submitButton_Click(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; if (Page.IsValid) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; submitButton.Text = "Valid"; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; submitButton.Text = "Invalid!"; &nbsp;&nbsp;&nbsp; } }</pre> <p><strong><span style="text-decoration: underline;">VB.Net </span></strong></p> <p>&nbsp;</p> <pre class="brush: vb">Protected Sub cv_name_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs) If lbl_iscitizen.Text.ToLower() = &#34;true&#34; Then args.IsValid = True Else args.IsValid = False End If End Sub Protected Sub submitButton_Click(ByVal sender As Object, ByVal e As EventArgs) If Page.IsValid Then submitButton.Text = &#34;Valid&#34; Else submitButton.Text = &#34;Invalid!&#34; End If End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><img src="https://i.imgur.com/EMi7BWS.gif" alt="" width="232" height="75" /></p>https://www.aspforums.net:443/Threads/147528/Validate-empty-TextBox-on-ASPNet-CustomValidator-ServerValidate-event-using-C-and-VBNet/https://www.aspforums.net:443/Threads/147528/Validate-empty-TextBox-on-ASPNet-CustomValidator-ServerValidate-event-using-C-and-VBNet/Thu, 22 Aug 2019 07:23:26 GMT