Validate empty TextBox on ASP.Net CustomValidator ServerValidate event using C# and VB.Net

Last Reply 28 days ago By dharmendr

Posted 28 days ago

Dear All,

I have created custom validator but it is not working on the condition 

if Iscitizen == true

then the textbox should be empty with no custom validator 

if Iscitizen == false

then the textbox custom validator is Required with text Required.

Thanks

<asp:Label ID="lbl_iscitizen" runat="server" Text="true"></asp:Label>
<asp:TextBox ID="lbl_iqamaexpiry" runat="server" Text=""  CssClass="textboxAslabel"></asp:TextBox>
<asp:CustomValidator ID="cv_name" runat="server" ControlToValidate="lbl_iqamaexpiry" Text="Required" OnServerValidate="cv_name_ServerValidate"
    ValidationGroup="validate" ForeColor="Red"></asp:CustomValidator>
<asp:Button ID="submitButton" runat="server" OnClick="submitButton_Click" Text="Submit" />

 

protected void cv_name_ServerValidate(object source, ServerValidateEventArgs args)
{
    if (lbl_iscitizen.Text == "true")
    {
        args.IsValid = false;
    }
    else
    {
        args.IsValid = true;
    }
}
protected void submitButton_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        submitButton.Text = "Valid";
    }
    else
    {
        submitButton.Text = "Invalid!";
    }
}

 

You are viewing reply posted by: dharmendr 28 days ago.
Posted 28 days ago Modified on 27 days ago

Hi irshad1231,

Since ControlToValidate is assigned as TextBox, CustomValidator will only fire when the TextBox is not empty.

So the work around for this is to set CustomValidator ValidateEmptyText property to true.

Check this example. Now please take its reference and correct your code.

For demonstration i have used TextBox instead of Label.

HTML

<asp:TextBox runat="server" ID="lbl_iscitizen" Text="true" /><br />
<%--<asp:Label ID="lbl_iscitizen" runat="server" Text="true"></asp:Label>--%>
<asp:TextBox ID="lbl_iqamaexpiry" runat="server" Text="" CssClass="textboxAslabel"></asp:TextBox>
<asp:CustomValidator ID="cv_name" runat="server" ControlToValidate="lbl_iqamaexpiry"
 Text="Required" ValidateEmptyText="true" OnServerValidate="cv_name_ServerValidate"
 ValidationGroup="validate" ForeColor="Red"></asp:CustomValidator><br />
<asp:Button ID="submitButton" runat="server" OnClick="submitButton_Click" Text="Submit"
 ValidationGroup="validate" />

C#

protected void cv_name_ServerValidate(object source, ServerValidateEventArgs args)
{
    if (lbl_iscitizen.Text.ToLower() == "true")
    {
        args.IsValid = true;
    }
    else
    {
        args.IsValid = false;
    }
}

protected void submitButton_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        submitButton.Text = "Valid";
    }
    else
    {
        submitButton.Text = "Invalid!";
    }
}

VB.Net

 

Protected Sub cv_name_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs)
    If lbl_iscitizen.Text.ToLower() = "true" 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 = "Valid"
    Else
        submitButton.Text = "Invalid!"
    End If
End Sub

Screenshot