Asp net customvalidator

ASP.NET is a powerful programming language that allows developers to create dynamic web . One task in web development is validating user input. In this article, we will explore how to use the ASP.NET CustomValidator control to validate user input in an ASP.NET application.

The CustomValidator control allows you to define your own logic by writing server-side code. This gives you the flexibility to implement complex validation rules that be achieved using the built-in validation controls.

To get started, let's consider a example where we want to validate a text input field to ensure that the user enters a valid email address. We can achieve this by using the CustomValidator control along with expressions.

First, let's add the CustomValidator control to our ASP.NET page. We can do this by adding the following code snippet within the


Step 1: Adding the CustomValidator control

In the above code, we have added a CustomValidator control with an ID of “EmailValidator”. We have also specified the ControlToValidate property to associate the validator with the “txtEmail” text input field. The ErrorMessage property is used to display an error message if the validation fails. Finally, we have specified the OnServerValidate handler, which we will implement in the next step.

Step 2: Implementing the validation logic

protected void EmailValidator_ServerValidate(object source, ServerValidateEventArgs args)
    string email = args.Value;
     regex = new Regex(@"^[w-]+(.[w-]+)*@([w-]+.)+[a-zA-Z]{2,7}$");
    args.IsValid = regex.IsMatch(email);

In the above code, we have implemented the EmailValidator_ServerValidate event handler. This event handler is called when the form is submitted and performs the validation logic. We retrieve the value of the text input field using the args.Value property. We then use a regular expression to validate the email address. If the email address is valid, we set args.IsValid to true; otherwise, we set it to .

Step 3: Displaying the validation result

In the above code, we have added a Label control with an ID of “lblResult”. This control will be used to display the validation result. We have set the property to false initially, as we only want to display the result after the form is submitted.

Step 4: Handling the form submission

protected void btnSubmit_Click(object sender, EventArgs e)
    if (Page.IsValid)
        lblResult.Text = "Form submitted successfully!";
        lblResult.Visible = true;

In the above code, we have implemented the btnSubmit_Click event handler. This event handler is called when the submit button is clicked. We check if the Page.IsValid property is true, which indicates that all the validation controls on the page have passed validation. If the validation is successful, we display a success message in the lblResult control.

Finally, let's add the submit button to our ASP.NET page:

Now, when the user enters an email address and clicks the submit button, the CustomValidator control will validate the input and display an error message if the email address is invalid. If the email address is valid, the form will be submitted successfully.

In conclusion, the ASP.NET CustomValidator control provides a flexible way to implement custom validation logic in ASP.NET applications. By using server-side code, we can validate user input based on complex rules. This allows us to create robust and secure web applications.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents