Asp.Net Validation Control

10537 ワード

  • Required Validator
  • Range Validator
  • RegularExpression Validator
  • Customer Validator
  • Compare Validator
  • Validation Summary

  • This Demo will show you how to use these validators .
    Aspx:
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestForValidation.aspx.cs"
        Inherits="WebCode.asp.net.Validation.TestForValidation" %>
    
    <!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>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="uplPanel" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <div>
                    <div>
                        This Field is For Validation Summary
                    </div>
                    <asp:ValidationSummary runat="server" ID="validationSummary" HeaderText="Please correct the following errors in the form:" />
                </div>
                <div>
                    <div>
                        This Field is For Required Validation
                    </div>
                    <asp:TextBox runat="server" ID="txtRequired"></asp:TextBox>
                    <asp:RequiredFieldValidator runat="server" ID="requireValidator" ControlToValidate="txtRequired"
                        Text="!" Display="Static" />
                </div>
                <div>
                    <div>
                        This Field Is For Customer Validation</div>
                </div>
                <asp:TextBox runat="server" ID="txtCustomer"></asp:TextBox>
                <br />
                <asp:CustomValidator ID="customerValidator" ControlToValidate="txtCustomer" Display="Static"
                    ErrorMessage="length can not more than 5 charactors !" OnServerValidate="ServerValidation"
                    runat="server" />
                <div>
                    <div>
                        This Field is For Range Validation
                    </div>
                    <asp:TextBox ID="txtRange" runat="server" />
                    <br />
                    <asp:RangeValidator ID="rangeValidator" ControlToValidate="txtRange" MinimumValue="1"
                        MaximumValue="10" Type="Integer" EnableClientScript="false" Text="The value must be from 1 to 10!"
                        runat="server" />
                </div>
                <div>
                    <div>
                        This Field is For Regular Expression Validation
                    </div>
                    <asp:TextBox runat="server" ID="txtRegular"></asp:TextBox>
                    <br />
                    <asp:RegularExpressionValidator ID="regularExpressionValidator" ControlToValidate="txtRegular"
                        ValidationExpression="\d{5}" Display="Static" ErrorMessage="must be 5 numeric digits"
                        EnableClientScript="False" runat="server" />
                </div>
                <div>
                    <div>
                        This Field is For Compared Validation
                    </div>
                    <div>
                        Operator :
                        <asp:ListBox ID="lstOperator" OnSelectedIndexChanged="Operator_Index_Changed" runat="server" AutoPostBack="True">
                            <asp:ListItem Selected="True" Value="Equal">Equal</asp:ListItem>
                            <asp:ListItem Value="NotEqual">NotEqual</asp:ListItem>
                            <asp:ListItem Value="GreaterThan">GreaterThan</asp:ListItem>
                            <asp:ListItem Value="GreaterThanEqual">GreaterThanEqual</asp:ListItem>
                            <asp:ListItem Value="LessThan">LessThan</asp:ListItem>
                            <asp:ListItem Value="LessThanEqual">LessThanEqual</asp:ListItem>
                            <asp:ListItem Value="DataTypeCheck">DataTypeCheck</asp:ListItem>
                        </asp:ListBox>
                         DataType :
                        <asp:ListBox ID="lstDataType" OnSelectedIndexChanged="Type_Index_Changed"
                        AutoPostBack="True" runat="server">
                            <asp:ListItem Selected="true" Value="String">String</asp:ListItem>
                            <asp:ListItem Value="Integer">Integer</asp:ListItem>
                            <asp:ListItem Value="Double">Double</asp:ListItem>
                            <asp:ListItem Value="Date">Date</asp:ListItem>
                            <asp:ListItem Value="Currency">Currency</asp:ListItem>
                        </asp:ListBox>
                    </div>
                    <div>
                        <asp:TextBox ID="txtCompareText1" runat="server" />
                        Compare to 
                        <asp:TextBox runat="server" ID="txtCompareText2"></asp:TextBox>
                    </div>
                    <asp:CompareValidator ID="compareValidator" ControlToValidate="txtCompareText1" ControlToCompare="txtCompareText2"
                        EnableClientScript="False" Type="String" runat="server" />
    
                </div>
                <div>
                    <asp:Button runat="server" Text="Save" ID="btnSave" OnClick="btnSave_Click" />
                    <asp:Button runat="server" Text="Submit" ID="btnSubmit" OnClick="btnSubmit_Click" />
                </div>
            </ContentTemplate>
            <Triggers>
                <asp:PostBackTrigger ControlID="btnSave" />
                <asp:PostBackTrigger ControlID="btnSubmit" />
            </Triggers>
        </asp:UpdatePanel>
        </form>
    </body>
    </html>
    

    Code Behind :
    using System;
    using System.Web.UI.WebControls;
    
    namespace WebCode.asp.net.Validation
    {
        public partial class TestForValidation : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    validationSummary.ValidationGroup = "Submit";
                    requireValidator.ValidationGroup = "Submit";
    
    
                    // btnSave.ValidationGroup = "Submit";//WON'T TRIGGER VALIDATION
                    btnSubmit.ValidationGroup = "Submit";
                }
            }
    
            protected void btnSubmit_Click(object sender, EventArgs e)
            {
                requireValidator.Validate();
                regularExpressionValidator.Validate();
                rangeValidator.Validate();
                TriggerCompareValidation();
                customerValidator.Validate();
    
                if (Page.IsValid)
                {
                    //Only Page is valid then submit     
                }
    
            }
    
            protected void btnSave_Click(object sender, EventArgs e)
            {
                // WON'T TRIIGER VALIDATION 
            }
    
            #region customer validation
            protected void ServerValidation(object source, ServerValidateEventArgs args)
            {
              	args.IsValid = args.Value.Length <= 5;
            }
    
            #endregion
    
            #region compare validation
    
            protected void TriggerCompareValidation()
            {
                switch (lstOperator.SelectedIndex)
                {
                    case 0:
                        compareValidator.Operator = ValidationCompareOperator.Equal;
                        break;
                    case 1:
                        compareValidator.Operator = ValidationCompareOperator.NotEqual;
                        break;
                    case 2:
                        compareValidator.Operator = ValidationCompareOperator.GreaterThan;
                        break;
                    case 3:
                        compareValidator.Operator = ValidationCompareOperator.GreaterThanEqual;
                        break;
                    case 4:
                        compareValidator.Operator = ValidationCompareOperator.LessThan;
                        break;
                    case 5:
                        compareValidator.Operator = ValidationCompareOperator.LessThanEqual;
                        break;
                    case 6:
                        compareValidator.Operator = ValidationCompareOperator.DataTypeCheck;
                        break;
                }
                switch (lstDataType.SelectedIndex)
                {
                    case 0:
                        compareValidator.Type = ValidationDataType.String;
                        break;
                    case 1:
                        compareValidator.Type = ValidationDataType.Integer;
                        break;
                    case 2:
                        compareValidator.Type = ValidationDataType.Double;
                        break;
                    case 3:
                        compareValidator.Type = ValidationDataType.Date;
                        break;
                    case 4:
                        compareValidator.Type = ValidationDataType.Currency;
                        break;
                }
                compareValidator.Validate();
            }
    
            protected void Operator_Index_Changed(object sender, EventArgs e)
            {
                TriggerCompareValidation();
            }
    
            protected void Type_Index_Changed(object sender, EventArgs e)
            {
                TriggerCompareValidation();
            }
            #endregion
    
    
        }
    }