Use validation groups in ASP.NET 2.0 and beyond : ValidationGroup « Validation by Control « ASP.Net

ASP.Net
1. ADO.net Database
2. Ajax
3. Asp Control
4. Collections
5. Components
6. Data Binding
7. Development
8. File Directory
9. HTML Control
10. Language Basics
11. Login Security
12. Mobile Control
13. Network
14. Page
15. Request
16. Response
17. Server
18. Session Cookie
19. Sitemap
20. Theme Style
21. User Control and Master Page
22. Validation by Control
23. Validation by Function
24. WebPart
25. WPF
26. 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 Tutorial
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 » Validation by Control » ValidationGroup 
Use validation groups in ASP.NET 2.0 and beyond

<%@ Page Language="C#" AutoEventWireup="true"%>

<!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>Validation groups</title>
</head>

<script language="javascript">
function CheckMembership(source, arguments)
{
  arguments.IsValid = false;
  var buf = arguments.Value;
  if (buf == "Normal" || buf== "Silver" ||
            buf == "Gold" || buf == "Platinum")
    arguments.IsValid = true;
}
</script>


<body>
    <div id="pageContent">
        <form id="form1" runat="server">
      <table>
          <tr>
              <td>Name</td><td>*</td>
            <td><asp:textbox runat="server" id="fname"  />
                <asp:RequiredFieldValidator runat="server" id="fnameValidator" ValidationGroup="NameGroup"
                    ControlToValidate="fname" 
                  Text="!!!"
                    ErrorMessage="Name is mandatory" /></td></tr>
          <tr>
              <td>Last Name</td><td>*</td>
            <td><asp:textbox runat="server" id="lname" />
                <asp:RequiredFieldValidator runat="server" id="lnameValidator" ValidationGroup="NameGroup"
                    ControlToValidate="lname" 
                  Text="!!!"
                    ErrorMessage="Last name is mandatory" /></td></tr>
          <tr><td colspan="3"><hr /></td></tr>
          <tr>
              <td>Age</td><td></td>
            <td><asp:textbox runat="server" id="age" text="15" />
                <asp:CompareValidator runat="server" id="ageValidator" 
                    ValidationGroup="PersonalDataGroup"
                    ControlToValidate="age" 
                  Operator="GreaterThanEqual" 
                  ValueToCompare="18"
                    Type="integer"
                    ErrorMessage="Age must be at least 18." /></td></tr>
          <tr>
              <td>Email</td><td></td>
            <td><asp:textbox runat="server" id="email" />
                <asp:RegularExpressionValidator runat="server" id="emailValidator" 
                    ValidationGroup="PersonalDataGroup"
                    ControlToValidate="email" 
                  ValidationExpression="[a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+" 
                    ErrorMessage="Must be an email address." /></td></tr>
          <tr>
              <td>Hire Date</td><td></td>
            <td><asp:textbox runat="server" id="hired"  />
                <asp:CompareValidator runat="server" id="hiredValidator" 
                       ValidationGroup="PersonalDataGroup"
                    ControlToValidate="hired" 
                  Display="Static"  
                  Operator="DataTypeCheck" 
                    Type="date"
                    ErrorMessage="Must enter a date." />
                <asp:RangeValidator runat="server" id="hiredDateValidator" 
                       ValidationGroup="PersonalDataGroup"            
                    ControlToValidate="hired" 
                  Display="Dynamic"
                    MinimumValue="2008-1-1"
                  MaximumValue="2008-12-31"
                    Type="Date"
                    ErrorMessage="Date after 1-1-99." /></td></tr>
          <tr>
              <td>Membership Level</td><td></td>
            <td><asp:textbox runat="server" id="membership" />
                <asp:CustomValidator runat="server" id="membershipValidator" 
                        ValidationGroup="PersonalDataGroup"            
                    ControlToValidate="membership" 
                  ClientValidationFunction="CheckMembership"
                    ErrorMessage="Must be Gold or Platinum." /></td></tr>
      </table>
      
        <hr>

      <asp:linkbutton ID="Linkbutton1" runat="server" Text="Validate Name..." ValidationGroup="NameGroup" />
      <asp:linkbutton ID="Linkbutton2" runat="server" Text="Validate Data..." ValidationGroup="PersonalDataGroup" />

        <hr>
        <asp:ValidationSummary ID="ValidationSummary1" runat="server"  
            ValidationGroup="NameGroup"
        ShowMessageBox="false" 
        ShowSummary="true" 
        HeaderText="The following errors occurred: (no message-box)"
        DisplayMode="List" />
        <asp:ValidationSummary ID="ValidationSummary2" runat="server"  
            ValidationGroup="PersonalDataGroup"
        ShowMessageBox="true" 
        ShowSummary="true" 
        HeaderText="The following errors occurred:"
        DisplayMode="BulletList" />
        </form>
    </div>
</body>
</html>

 
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.