In this article we will learn how to use CustomValidator control in ASP. NET.
CustomValidator control:
The CustomValidator control calls a user-defined function to perform validations that the standard validators can't handle. The interesting stuff in this control is that you have ability to validate your input data both from Client side using JavaScript and Server side by writing your code in code behind file in any of the .NET supported programming language.
Properties:
These are the following properties of the CustomValidator control.
Figure 1.
Text - Text to display for the validator when the validated control is invalid.
BackColor - The background color of the RequiredFieldValidator control.
ControlToValidate - The id of the control to validate.
Display - The display behavior for the validation control. Legal values are:
-None (the control is not displayed. Used to show the error message only in the ValidationSummary control).
-Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation.
-Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation.
ErrorMessage - The text to display in the ValidationSummary control when validation fails. Note: This text will also be displayed in the validation control if the Text property is not set.
ForeColor - The foreground color of the control.
For example:
Drag a Label control, one TextBox control, Button control from the toolbox on the form. The form looks like this.
Figure 2.
Now click on the source button of the design form to see the code.
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication32.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<h3><font face="Verdana">CustomValidator Example</font></h3>
<form id="form1" runat="server">
<div>
<asp:Label id="lblOutput" runat="server"
Text="Enter an even number:"
Font-Names="Verdana"
Font-Size="10pt" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CustomValidator id="CustomValidator1" runat="server"
ControlToValidate="TextBox1"
OnServerValidate="ServerValidate"
Display="Static"
Font-Names="verdana" Font-Size="10pt" SetFocusOnError="True" ForeColor="Red">
Not an even number!
</asp:CustomValidator>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Validate" />
</div>
</form>
</body>
</html>
Now defines a user defined function to check the number is prime. Double click on the button(validate) of the design form and add the following code.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
If (Page.IsValid) Then
lblOutput.Text = "Page is valid!"
Else
lblOutput.Text = "Page is not valid! :-("
End If
End Sub
Sub ServerValidate(ByVal sender As Object, ByVal value As ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
Dim num As Integer
' even number?
If Integer.TryParse(value.Value, num) Then
value.IsValid = (num Mod 2 = 0)
Else
value.IsValid = False
End If
End Sub
Now save and run the application.
Figure 3.
Now suppose we enter 3 which is not an even number it will shows an error.
Figure 4.
Now suppose we enter 6 which is an even number.
Figure 5.