Working with CustomValidator control in ASP.NET
Posted by Sheo Narayan
on 8/19/2009 for Beginner level | Views : 104438
If you found plagiarised (copied)
or inappropriate content,
please let us know
the original source along with your correct email id (to communicate) for further action.
This article shows how to use CustomValidator validation control that is available in ASP.NET out of the box.
The basic tutorials about this control had been written by me long back that is available here. In this article, we shall try to dig more into this control.
Creating a Simple Form
Drag and drop a TextBox, Button and a Custom Validator control from the toolbar or you may prefer copying below code into your page as well to see how this control works.
<asp:TextBox ID="txtEmail" AutoCompleteType="None" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CV1" runat="Server" ClientValidationFunction="JSValidate"
OnServerValidate="ServerValidate" Text="At least 8 character long"
ControlToValidate="txtEmail" ValidateEmptyText="True" ErrorMessage="Validation Failed"></asp:CustomValidator>
<asp:Label ID="lblMessage" ForeColor="brown" runat="Server" EnableViewState="False" />
<asp:Label ID="lblError" ForeColor="red" runat="Server" EnableViewState="False" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="SubmitThisForm" />
Notice the CustomValidator control here and focus on two properties called ClientValidationFunction and OnServerValidate. As their name suggests, the first one is to validate the input value of the TextBox from client side (JSValidate function) and other is to validate the input value from server side (ServerValidate method).
Lets see code for both functions here.
Client Side Function
When the Submit button will be clicked, first ClientValidationFunction function will fire that will try to validate the length of the characters entered into the TextBox, refer to the code below.
function JSValidate(source, args)
var element = document.getElementById('<%=txtEmail.ClientID %>');
if (element.value.length >= 8) // you can also write args.Value
args.IsValid = true;
args.IsValid = false;
Note: It is important to maintain the definition of the function both at client side and server side. You can notice that above function has two parameter called source and args. As we are worried about the TextBox value here so first we need to get the id of the TextBox, I have got it using ClientID property of the TextBox control and have stored into the element variable. In the following line I am checking the length of the TextBox to see if its more than 8 characters and setting the value of IsValid property of the args parameter to true / false.
Setting the IsValid property of args tells the Validator function whether the client side validation has failed or passed. If failed, it will show the error Text specified in the CustomValidator control and will not submit the form else the form will be submitted to the server.
Get solutions of .NET problems with video explanations, .pdf and source code in .NET How to's.
Server Side Function
protected void ServerValidate(object source, ServerValidateEventArgs args)
RegexStringValidator r = new RegexStringValidator(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
args.IsValid = true;
catch (ArgumentException ee)
lblError.Text = "Error occured: Doesn't seem to be valid email id. <br />" +
args.IsValid = false;
protected void SubmitThisForm(object sender, EventArgs e)
lblMessage.Text = "Success: The TextBox value is: <b>" + txtEmail.Text + "</b>";
Irrespective of the result of the Server side validation method, the onclick event of the Submit button will fire. To avoid processing the form in the OnClick event in case of Server side validation failed, we need to check for the validity of the page by using Page.IsValid, this ensures that all validation control placed in the .aspx page has passed successfully. If Page.IsValid returns true then we can go ahead with the processing of the form as shown in the SubmitThisForm method.
Hope this simple and basic article will help beginners to know how to use CustomValidator controls in ASP.NET web forms. please subscribe for the subsequent article alert directly in your email box. Thanks and Happy coding!
Found interesting? Add this to: