Home > ASP.Net > Use CustomValidator To Validate Empty TextBoxes

Use CustomValidator To Validate Empty TextBoxes


try this Example to validate Empty textbox and change it border color

if it empty to Red

Step 1 : create new web site and add new page

Step2 : add two TextBoex and two CustomValidator controls and ValidationSummary control and a Button in your page

<script type="text/javascript">
    function vali(source, args)
    if(document.getElementById(source.controltovalidate).value.length > 0)
      args.IsValid = true;
      args.IsValid =false;

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox1"
            ErrorMessage="Enter First Name" SetFocusOnError="True" ClientValidationFunction="vali" ValidateEmptyText="True" Display="None">Enter First Name</asp:CustomValidator><br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator2" runat="server" ClientValidationFunction="vali"
            ControlToValidate="TextBox2" Display="None" ErrorMessage="Enter Second Name"
            SetFocusOnError="True" ValidateEmptyText="True">Enter Second Name</asp:CustomValidator><br />
        <br />
        <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"
            ShowSummary="False" />
        <br />

        <asp:Button ID="Button1" runat="server" Text="Button" />

Hope this helps

Good Luck

Categories: ASP.Net Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: