Using CustomValidator Control

In this example we will use the CustomValidator controls client-side and server-side abilities to perform validation on the password and email form values. The client-side javascript will perform some basic checks on the password, like length and against a reserved word list. The server-side validation will check the email address against already registered emails.









The form above demonstrates a number of different type of form validation. We will focus on the custom validation, on one we set the ClientValidationFunction property to the name of the javascript function which will handle the client-side validation.

Below is the client-side code. The function receives two arguments, the second has a Value property which corresponds to the value property of the input field being validated. You set the IsValid property to true\false based on your validation tests.

function ValidatePassword(source, arguments) {
    arguments.IsValid = false;

    if (arguments.Value == 'password')

    arguments.IsValid = true;

Below is the server-side code. You need to handle the ServerValidate event. Again the arguments.Value property has the value of the input field and you set the IsValid property to true\false based on your validation tests.

namespace CustomValidatorTestApp
    public partial class _Default : Page
        protected void Page_Load(object sender, EventArgs e)
            lbMessage.Text = "";
            BtnRegister.Click += BtnRegister_Click;
            cvEmail.ServerValidate += CvEmailServerValidate;

        protected void CvEmailServerValidate(object source, ServerValidateEventArgs args)
            args.IsValid = false;

            bool isRegistered = false;

            string[] emails = new string[] {"",""};

            foreach (string email in emails)
                if (email.ToLower() == args.Value.Trim().ToLower())
                    isRegistered = true;

            args.IsValid = !isRegistered;

        protected void BtnRegister_Click(object sender, EventArgs e)
            if (!Page.IsValid)

            //your logic here

            lbMessage.Text = "Registration complete.";

You can download the Using CustomValidator Control project here


No comments yet.

Add Yours

  • Author Avatar


Comment Arrow

About Author


Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning hands down.