Onlineatoz.net | ToolBox Validation Control CustomValidator

ToolBox Validation Control CustomValidator

CustomValidator Web Server Control

in ASP.NET environment,This article describes you how to work with CustomValidator Web Control.This control allows you to verify the entered value at run time.and also Checks the user's entry using validation logic that you write yourself. This type of validation enables you to check for values derived at run time.

The CustomValidator control is a separate control from the input control it validates, which allows you to control where the validation message is displayed.in ASP.NET Validation controls perform validation on the server everytime.

The following code snippets explains you the RegularExpressionValidator control

<asp:CustomValidator ID="CustomValidator1" ControlToValidate="txt1"
 Display="Static"
    ErrorMessage="not ok!" ForeColor="green" Font-Name="verdana"
 Font-Size="10pt"
    OnServerValidate="ServerValidation" runat="server" />
 
Formating the CustomValidator

ASP.NET validation controls allows you to customizing the format—font, size, and so on—of error text, or you can substitute a marker for error text. For example, you can have the validation control display an asterisk (*) when an error occurs.

You can also include a detailed error message in the ErrorMessage property of the validation control and add a ValidationSummary control to the page. The detailed ErrorMessage property text will appear on the page in the location of the ValidationSummary control.

To format error messages, specify the following properties,
  • ForeColor - The color of the error message text.
  • BackColor -The color behind the text.
  • Font -The font face, size, weight, and so on.
  • BorderWidth, BorderColor, and BorderStyle -The size and color of a border around the error message.

The following example demonstrates how to use the CustomValidator

in .aspx.cs page,

void ButtonClick(Object sender, EventArgs e)
      {
         if (Page.IsValid)
         {
            Label1.Text="valid page";
         }
         else
         {
            Label1.Text="not valid page!";
         }
      }
void meth1(object source, ServerValidateEventArgs args)
      {
         try 
         {
            int i = int.Parse(args.Value);
            args.IsValid = ((i%2) == 0);
         }

         catch(Exception ex)
         {

            args.IsValid = false;

         }
      }
        

in .aspx page,

<form id="Form2" runat="server">
    <h3>
        CustomValidator ServerValidate Example</h3>
    <asp:Label ID="Message" Text="specify no:" Font-Name="Verdana"
 Font-Size="10pt"
        runat="server" />
    <p>
        <asp:TextBox ID="txt1" runat="server" />
          
        <asp:CustomValidator ID="CustomValidator1" ControlToValidate="txt1"
 Display="Static"
            ErrorMessage="not ok!" ForeColor="green" Font-Name="verdana"
 Font-Size="10pt"
            OnServerValidate="ServerValidation" runat="server" />
    <p>
        <asp:Button ID="Button1" Text="Validate" OnClick="ValidateBtn_OnClick"
 runat="server" />
</form>
     
 
Related Links

Posted by: Admin
Posted on: 9/13/2011 at 6:43 PM
Categories: Asp.net
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed