Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to use CustomValidator Control and write Custom Validation function in ASP.NET

Author: Usman Lateef

CustomValidation control allows you to write your own logic for validation. This article will explain how you can write your own client side validation functions.

In other article of What are the ASP.NET Validation Controls and how to use them for client side validation, it was briefly explained about validation controls and described these validation controls in an example code. If you want to know what the other validation controls are, please refer to above mentioned article.


By using first four ASP.NET validation controls (RequiredFieldValidator, CompareValidator,  RangeValidator and RegularExpressionValidator), you will be able to validate most of the validation needs but sometimes you need more complex validation by using your own defined validation logic. CustomValidator provides the facility to write custom functions to validate user inputs.  You can also write server side validation function for CustomValidator by using OnServerValidate property but in this article you will learn to write client side validation function.
Follow below steps:
  1. Create a new empty website in Visual Studio 2010 either in C# or in VB.NET
  2. Add a new item (Web Form) in the website
  3. Copy and paste following HTML code in .aspx page . ControlToValidate property of CustomValidator is set to TextBox1.

    Enter a number:

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:CustomValidator ID="CustomValidator1" runat="server"
    ControlToValidate="TextBox1" Display="Dynamic"
    ErrorMessage="Number must be divisible by 2" SetFocusOnError="True" ClientValidationFunction="validateNumber">Number must be divisible by 2</asp:CustomValidator>

    <br />

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

     <br />

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>


  5. Now add following <script> tag and JavaScript function in .aspx page under <head> tag. This JavaScript function will check that the number entered in TextBox1 is divisible by two. ClientValidationFunction property of CustomValidator control is set to validateNumber which is name of the function.

    <script type="text/javaScript">
       function validateNumber(oSrc, args) {
       args.IsValid = (args.Value % 2 == 0);

  6. Now write the following button click event in the code behind file
    protected void Button1_Click(object sender, EventArgs e) {
            Label1.Text = "Number is: " + TextBox1.Text + ", Which is divisible by 2"; 
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Label1.Text = "Number is: " + TextBox1.Text + ", Which is divisible by 2"
    End Sub
  7. View the web page in browser, enter any number in text box and click on button to check custom validation function.
I have explained the CustomValidator JavaScript function in simple way but you can add more complex logic to JavaScript function for more advanced validation.


Add Article Comment:
Name :
Email Address :
Comments :
<< How to use SMTP server to send email in ASP.NET

Disclaimer - Privacy
© 2002-2017