共用方式為


RegularExpressionValidator 控制項

評估輸入控制項的值,以判斷是否符合規則運算式所定義的模式。

<asp:RegularExpressionValidator 
     id="ProgrammaticID" 
     ControlToValidate="ProgrammaticID of control to validate" 
     ValidationExpression="expression" 
     ErrorMessage="Message to display in ValidationSummary control"
     Text="Message to display in control"
     ForeColor="value" 
     BackColor="value" ... 
     runat="server" >
</asp: RegularExpressionValidator>

備註

RegularExpressionValidator 控制項是用來判斷輸入控制項的值是否符合規則運算式所定義的模式。這種類型的驗證允許您檢查可預測的字元序列,例如社會福利號碼、電子郵件地址、電話號碼和郵遞區號等等。

**注意   **如果輸入控制項為空白,則沒有驗證函式會被呼叫,並且驗證成功。使用 RequiredFieldValidator 控制項來避免使用者略過輸入控制項。

除非瀏覽器不支援用戶端驗證或明確停用用戶端驗證 (EnableClientScript 屬性設定為 false),否則執行伺服器端和用戶端驗證。

使用 ValidationExpression 屬性來指定用於輸入控制項的規則運算式。規則運算式驗證語法在用戶端和伺服器上略有不同。在用戶端,使用 JScript 規則運算式語法。在伺服器上,使用 Regex 語法。因為 JScript 規則運算式語法是 Regex 語法的子集合,建議使用 JScript 規則運算式語法,以便在用戶端和伺服器兩邊產生相同的結果。

如需建立和格式化規則運算式的詳細資訊,請參閱規則運算式

如需 RegularExpressionValidator 控制項的詳細資訊,請參閱 RegularExpressionValidator 類別。

範例

下列範例示範如何使用 RegularExpressionValidator 控制項來驗證輸入至特定模式之文字方塊中的值。在這個範例中,模式是具有五位數字的郵遞區號。驗證結果然後會被顯示在網頁上。

<%@ Page Language="VB" AutoEventWireup="True" %>

 <html>
 <head>
    <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 Sample</h3>
    <p>
 
    <form runat="server">
 
       <table bgcolor="#eeeeee" cellpadding="10">
          <tr valign="top">
             <td colspan="3">
                <asp:Label id="lblOutput" 
                     Text="Enter a 5 digit zip code" 
                     Font-Name="Verdana" 
                     Font-Size="10pt" 
                     runat="server"/>
             </td>
          </tr>
 
          <tr>
             <td colspan="3">
                <font <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"
                     EnableClientScript="false"
                     ErrorMessage="Zip code must be 5 numeric digits"
                     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>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>

 <html>
 <head>
    <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>
 
    <h3>RegularExpressionValidator Sample</h3>
    <p>
 
    <form runat="server">
 
       <table bgcolor="#eeeeee" cellpadding="10">
          <tr valign="top">
             <td colspan="3">
                <asp:Label id="lblOutput" 
                     Text="Enter a 5 digit zip code" 
                     Font-Name="Verdana" 
                     Font-Size="10pt" 
                     runat="server"/>
             </td>
          </tr>
 
          <tr>
             <td colspan="3">
                <font <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"
                     EnableClientScript="false"
                     ErrorMessage="Zip code must be 5 numeric digits"
                     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 類別 | 驗證伺服器控制項規則運算式