Change TextBox Color on Validation Fail in Asp.net

Follow this simple steps
1. write Following javascript code on  head tag

    function changeColor(source, args)
        {
     
            var txtuser = document.getElementById('<%= txtUserName.ClientID %>');
            if(args.Value.length == 0)
            {
                txtuser.style.border="red solid 1px";
                args.IsValid = false;
            }
            else
            {
                txtuser.style.background = 'white';
                args.IsValid = true;
            }
        }


2. Drag Textbox on Form give id="txtUserName"

<asp:TextBox ID="txtUserName" runat="server" ></asp:TextBox>

3. Drag CustomValidator control from toolbox.Set Following Properties

<asp:CustomValidator ID="CustomValidator1" runat="server"  ControlToValidate="txtUserName"
            ErrorMessage="" ValidateEmptyText="True"
            ClientValidationFunction="changeColor"></asp:CustomValidator>


4.Take one Button on form.

5. Run Website.


Ebook Download
View all
Learn
View all