Validate a form : Form « Validation « ASP.NET Tutorial

ASP.NET Tutorial
1. ASP.Net Instroduction
2. Language Basics
3. ASP.net Controls
4. HTML Controls
5. Page Lifecycle
6. Response
7. Collections
8. Validation
9. Development
10. File Directory
11. Sessions
12. Cookie
13. Cache
14. Custom Controls
15. Profile
16. Configuration
17. LINQ
18. ADO.net Database
19. Data Binding
20. Ajax
21. Authentication Authorization
22. I18N
23. Mobile
24. WebPart
25. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.NET Tutorial » Validation » Form 
8. 6. 1. Validate a form
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Validators" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
<script language="JavaScript">
  function EmpIDClientValidate(ctl, args)
  {
    args.IsValid=(args.Value%== 0);
  }
</script>

</head>
<body>
    <form id="form1" runat="server">
  Name:<asp:TextBox runat="server" Width="200px" ID="Name" />
     <asp:RequiredFieldValidator runat="server" 
                                ID="ValidateName" 
                                ControlToValidate="Name" 
                                ErrorMessage="Name is required"
                              Display="dynamic">*
    </asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator runat="server" 
                                    ID="ValidateName2" 
                                    ControlToValidate="Name" 
                                    validationExpression="[a-z A-Z]*"
                                  ErrorMessage="Name cannot contain digits" 
                                  Display="dynamic">*
      </asp:RegularExpressionValidator>
    ID (multiple of 5):
        <asp:TextBox runat="server" Width="200px" ID="EmpID" />
    <asp:RequiredFieldValidator runat="server" 
                                ID="ValidateEmpID" 
                                ControlToValidate="EmpID" 
                                ErrorMessage="ID is required"
                                    Display="dynamic">*
    </asp:RequiredFieldValidator>
    <asp:CustomValidator runat="server" 
                         ID="ValidateEmpID2" 
                         ControlToValidate="EmpID" 
                         ClientValidationFunction="EmpIDClientValidate"
                             ErrorMessage="ID must be a multiple of 5" 
                             Display="dynamic" 
                             OnServerValidate="ValidateEmpID2_ServerValidate">*
      </asp:CustomValidator>
    Day off:<small>08/08/08-08/20/08</small>
    <asp:TextBox runat="server" Width="200px" ID="DayOff" />
    <asp:RequiredFieldValidator runat="server" 
                                ID="ValidateDayOff" 
                                ControlToValidate="DayOff" 
                                ErrorMessage="Day Off is required"
                                    Display="dynamic">*
    </asp:RequiredFieldValidator>
    <asp:RangeValidator runat="server" 
                        ID="ValidateDayOff2" 
                        ControlToValidate="DayOff" 
                        MinimumValue="08/08/2008"
                      MaximumValue="08/20/2008" 
                      Type="Date" 
                      ErrorMessage="Day Off is not within the valid interval" 
                      Display="dynamic" 
                      SetFocusOnError="True">*
    </asp:RangeValidator>
    Age&nbsp<small>>= 18 )</small>:</td>
    <asp:TextBox runat="server" Width="200px" ID="Age" />
    <asp:RequiredFieldValidator runat="server" 
                                ControlToValidate="Age" 
                                ErrorMessage="Age is required" 
                                Display="dynamic"
                              ID="Requiredfieldvalidator1" 
                              Name="Requiredfieldvalidator1">*
    </asp:RequiredFieldValidator>
    <asp:CompareValidator runat="server" 
                          ID="ValidateAge" 
                          ControlToValidate="Age" 
                          ValueToCompare="18" 
                          Type="Integer"
                        Operator="GreaterThanEqual" 
                        ErrorMessage="You must be at least 18-year-old" 
                        Display="dynamic">*
      </asp:CompareValidator>
    E-mail:
    <asp:TextBox runat="server" Width="200px" ID="Email" />
    <asp:RequiredFieldValidator runat="server" 
                                ControlToValidate="Email" 
                                ErrorMessage="E-mail is required" 
                                Display="dynamic"
                              ID="Requiredfieldvalidator2" 
                              Name="Requiredfieldvalidator2">*
    </asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator runat="server" 
                                    ID="ValidateEmail" 
                                    ControlToValidate="Email" 
                                    validationExpression=".*@.{2,}\..{2,}"
                                  ErrorMessage="E-mail is not in a valid format" Display="dynamic">*
      </asp:RegularExpressionValidator>
    Password:
    <asp:TextBox TextMode="Password" runat="server" Width="200px" ID="Password" />
    <asp:RequiredFieldValidator runat="server" 
                                ControlToValidate="Password" 
                                ErrorMessage="Password is required"
                                    Display="dynamic" 
                                    ID="Requiredfieldvalidator3" 
                                    Name="Requiredfieldvalidator3">
    <img src="http://www.java2java.com/style/logo.png" border="0">
    </asp:RequiredFieldValidator>
    Re-enter Password:
    <asp:TextBox runat="server" TextMode="Password" Width="200px" ID="Password2" />
    <asp:RequiredFieldValidator runat="server" 
                                ControlToValidate="Password2" 
                                ErrorMessage="Password2 is required"
                              Display="dynamic" 
                              ID="Requiredfieldvalidator4" 
                              Name="Requiredfieldvalidator4">
    <img src="http://www.java2java.com/style/logo.png" border="0">
    </asp:RequiredFieldValidator>
    <asp:CompareValidator runat="server" 
                          ControlToValidate="Password2" 
                          ControlToCompare="Password" 
                          Type="String"
                ErrorMessage="The passwords don't match" 
                Display="dynamic" 
                ID="Comparevalidator1" 
                Name="Comparevalidator1">
      <img src="imgError.gif" border="0">
    </asp:CompareValidator>
    <asp:Button runat="server" Text="Submit" ID="Submit" OnClick="Submit_Click" /><BR>
    <asp:CheckBox runat="server" ID="EnableValidators" Checked="True" AutoPostBack="True" Text="Validators enabled" OnCheckedChanged="OptionsChanged" />
    <asp:CheckBox runat="server" ID="EnableClientSide" Checked="True" AutoPostBack="True" Text="Client-side validation enabled" OnCheckedChanged="OptionsChanged" />
    <asp:CheckBox runat="server" ID="ShowSummary" Checked="True" AutoPostBack="True" Text="Show summary" OnCheckedChanged="OptionsChanged" />
    <asp:CheckBox runat="server" ID="ShowMsgBox" Checked="False" AutoPostBack="True" Text="Show message box" OnCheckedChanged="OptionsChanged" />
    <asp:ValidationSummary runat="server" ID="ValidationSum" DisplayMode="BulletList" HeaderText="<b>Please review the following errors:</b>"
        ShowSummary="true" />
    <asp:Label runat="server" ID="Result" ForeColor="magenta" Font-Bold="true" EnableViewState="False" />
    </div>
    </form>
</body>
</html>


File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Validators : System.Web.UI.Page
{
  protected void Submit_Click(object sender, EventArgs e)
  {
    if (Page.IsValid)
      Result.Text = "Thanks for sending your data";
    else
      Result.Text = "There are some errors, please correct them and re-send the form.";
  }
  protected void OptionsChanged(object sender, EventArgs e)
  {
    foreach (BaseValidator valCtl in Page.Validators)
    {
      valCtl.Enabled = EnableValidators.Checked;
      valCtl.EnableClientScript = EnableClientSide.Checked;
    }
    ValidationSum.ShowMessageBox = ShowMsgBox.Checked;
    ValidationSum.ShowSummary = ShowSummary.Checked;
  }
  protected void ValidateEmpID2_ServerValidate(object source, ServerValidateEventArgs args)
  {
    try
    {
      args.IsValid = (int.Parse(args.Value== 0);
    }
    catch
    {
      args.IsValid = false;
    }
  }
}
8. 6. Form
8. 6. 1. Validate a form
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.