Regular Expression based validation (C#) : Regular Expression « Validation « ASP.NET Tutorial






File: Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="RegularExpressionTest" %>

<!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>Regular Expression Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:textbox id="txtExpression" 
                     runat="server" 
                     Width="192px"></asp:textbox>
        <asp:button id="cmdSetExpression" 
                    runat="server" 
                    Width="138px" 
                    Text="Set This Expression" 
                    CausesValidation="False" 
                    OnClick="cmdSetExpression_Click">
        </asp:button>
        <asp:label id="lblExpression" 
                   runat="server" 
                   Height="21px" 
                   Width="512px" >Current Expression: (none)</asp:label>
        <asp:label id="Label1" 
                   runat="server" >New Expression:</asp:label>
    
    <div>
      <asp:validationsummary id="ValidationSummary1" runat="server" ></asp:validationsummary>
      <asp:regularexpressionvalidator id="TestValidator" 
                                      runat="server" 
                                      EnableClientScript="False" 
                                      ErrorMessage="Failed Validation" 
                                      ControlToValidate="txtValidate"></asp:regularexpressionvalidator>
      <asp:textbox id="txtValidate" runat="server" Width="192px"></asp:textbox>
      <asp:button id="cmdValidate" runat="server"  Width="139px" Text="Validate"></asp:button>
      <asp:label id="Label2" runat="server" Height="16px" Width="184px" >Test Current Expression:</asp:label>
    </div>
    </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 RegularExpressionTest : System.Web.UI.Page
{

  protected void cmdSetExpression_Click(object sender, EventArgs e)
  {
    TestValidator.ValidationExpression = txtExpression.Text;
    lblExpression.Text = "Current Expression: ";
    lblExpression.Text += txtExpression.Text;

  }
}








8.9.Regular Expression
8.9.1.The predefined character classes
8.9.2.quantifiers used to create patterns that match a variable number of characters at a certain position in the string
8.9.3.Metacharacters for Matching Single Characters
8.9.4.Metacharacters for Matching Types of Characters
8.9.5.Quantifiers
8.9.6.Commonly Used Regular Expressions
8.9.7.RegularExpressionValidator compares value against a regular expression
8.9.8.Regular Expression based validation (C#)
8.9.9.Use asp:RegularExpressionValidator to check the zip code
8.9.10.Use asp:RegularExpressionValidator to check the email address
8.9.11.Use Regular expression to validate a Phone number