RegularExpressionValidator.ValidationExpression プロパティ

定義

フィールドの検証に使用するパターンを決定する正規表現を取得または設定します。

public:
 property System::String ^ ValidationExpression { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Bindable(true)]
public string ValidationExpression { get; set; }
[System.Web.UI.Themeable(false)]
public string ValidationExpression { get; set; }
[<System.ComponentModel.Bindable(true)>]
member this.ValidationExpression : string with get, set
[<System.Web.UI.Themeable(false)>]
member this.ValidationExpression : string with get, set
Public Property ValidationExpression As String

プロパティ値

フィールドの書式の検証に使用する正規表現を指定する文字列。 既定値は、Empty です。

属性

例外

正規表現の書式が正しくありません。

次の例では、 プロパティを使用して 5 桁の ValidationExpression 数値を持つフィールドを検証する方法を示します。 コントロールがButtonクリックされると、結果OnClickのイベント ハンドラーは の プロパティをIsValidチェックして、 内TextBoxPageテキストが正規表現を満たすかどうかを判断します。

重要

この例には、ユーザー入力を受け付けるテキスト ボックスがあります。これにより、セキュリティが脆弱になる可能性があります。 既定では、ASP.NET Web ページによって、ユーザー入力にスクリプトまたは HTML 要素が含まれていないかどうかが検証されます。 詳細については、「スクリプトによる攻略の概要」を参照してください。

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

 <!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>
    <title>RegularExpressionValidator Example</title>
<script runat="server">
 
       void ValidateBtn_Click(Object sender, EventArgs e) 
       {
          if (Page.IsValid) 
          {
             lblOutput.Text = "Page is Valid.";
          }
          else 
          {
             lblOutput.Text = "Page is InValid.";
          }
       }
 
    </script>

 </head>
 <body> 
    <form id="form1" runat="server">
 
    <h3>RegularExpressionValidator Example</h3>

       <table style="background-color:#eeeeee; padding:10">
          <tr valign="top">
             <td colspan="3">
                <asp:Label ID="lblOutput" 
                     Text="Enter a 5-digit ZIP Code" 
                     runat="server"
                     AssociatedControlID="TextBox1"/>
             </td>
          </tr>
 
          <tr>
             <td colspan="3">
                <b>Personal Information</b>
             </td>
          </tr>
          <tr>
             <td align="right">
                Zip Code:
             </td>
             <td>
                <asp:TextBox id="TextBox1" 
                     runat="server"/>
             </td>
             <td>
                <asp:RegularExpressionValidator id="RegularExpressionValidator1" 
                     ControlToValidate="TextBox1"
                     ValidationExpression="\d{5}"
                     Display="Static"
                     ErrorMessage="ZIP code must be 5 numeric digits"
                     EnableClientScript="False" 
                     runat="server"/>
             </td>
          </tr>
          <tr>
             <td></td>
             <td>
                <asp:Button text="Validate" 
                     OnClick="ValidateBtn_Click" 
                     runat="server" />
             </td>
             <td></td>
          </tr>
       </table>
 
    </form>
 </body>
 </html>
<%@ Page Language="VB" AutoEventWireup="True" %>

 <!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>
    <title>RegularExpressionValidator Example</title>
<script runat="server">
 
       Sub ValidateBtn_Click(sender As Object, e As EventArgs) 
       
          If Page.IsValid Then 
          
             lblOutput.Text = "Page is Valid."
          
          Else 
          
             lblOutput.Text = "Page is InValid."

          End If

       End Sub
 
    </script>
 
 </head>
 <body>
 
    <h3>RegularExpressionValidator Example</h3>
    <br />
 
    <form id="form1" runat="server">
 
       <table style="background-color:#eeeeee; padding:10">
          <tr valign="top">
             <td colspan="3">
                <asp:Label ID="lblOutput" 
                     Text="Enter a 5-digit ZIP Code" 
                     runat="server"
                     AssociatedControlID="TextBox1"/>
             </td>
          </tr>
 
          <tr>
             <td colspan="3">
                <b>Personal Information</b>
             </td>
          </tr>
          <tr>
             <td align="right">
                Zip Code:
             </td>
             <td>
                <asp:TextBox id="TextBox1" 
                     runat="server"/>
             </td>
             <td>
                <asp:RegularExpressionValidator id="RegularExpressionValidator1" 
                     ControlToValidate="TextBox1"
                     ValidationExpression="\d{5}"
                     Display="Static"
                     ErrorMessage="Zip code must be 5 numeric digits"
                     EnableClientScript="False" 
                     runat="server"/>
             </td>
          </tr>
          <tr>
             <td></td>
             <td>
                <asp:Button text="Validate" 
                     OnClick="ValidateBtn_Click" 
                     runat="server" />
             </td>
             <td></td>
          </tr>
       </table>
 
    </form>
 
 </body>
 </html>

注釈

このプロパティを使用して、社会保障番号、電子メール アドレス、電話番号、郵便番号など、予測可能な文字シーケンスのチェックに使用するパターンを指定します。

RegularExpressionValidator では、空の文字列の検証は実行されません。 テストする文字列が空である可能性がある場合は、 と を使用 RequiredFieldValidator します RegularExpressionValidator

正規表現の詳細については、「正規表現の.NET Framework」を参照してください。

Note

パターン マッチング コンストラクトで問題が発生した場合は、式を "^(" と ")$" でラップしてみてください。 たとえば、"a|ab" は "^(a|ab)$" になります。

このプロパティは、テーマまたはスタイル シート テーマによって設定することはできません。 詳細については、「テーマとスキン」と「ASP.NET」を参照してくださいThemeableAttribute

適用対象