Validation服务器控件:RegularExpressionValidator控件


阅读此文请先查看网页教学网的:ASP.NET入门教程:Validation服务器控件,简单讲述了Web服务器控件的使用方法。

定义和用法

RegularExpressionValidator 控件用于验证输入值是否匹配正则表达式指定的模式。计算输入控件的值,以确定该值是否与某个正则表达式所定义的模式相匹配。

注释:除非浏览器不支持客户端验证或 EnableClientScript 被设置为 false,否则将同时执行服务器端和客户端验证。

注释:如果输入控件为空,验证将失败。请使用 RequiredFieldValidator 控件,使字段成为必选字段。

属性

属性 描述
BackColor RangeValidator 控件的背景颜色
ControlToValidate 要验证的控件的 id
Display 验证控件的显示行为。

合法的值有:

  • None - 验证消息从不内联显示。
  • Static - 在页面布局中分配用于显示验证消息的空间。
  • Dynamic - 如果验证失败,将用于显示验证消息的空间动态添加到页面。
EnableClientScript 布尔值,规定是否启用客户端验证。
Enabled 布尔值,规定是否启用验证控件。
ErrorMessage

当验证失败时,在 ValidationSummary 控件中显示的文本。

注释:如果未设置 Text 属性,文本也会显示在该验证控件中。

ForeColor 该控件的前景色。
id 控件的唯一 id。
IsValid 布尔值,指示关联的输入控件是否通过验证。
runat 规定该控件是一个服务器控件。必须设置为 "server"。
Text 当验证失败时显示的消息。
ValidationExpression 规定验证输入控件的正则表达式。在客户端和服务器上,表达式的语法是不同的。

语法

<asp:RegularExpressionValidator
    AccessKey="string"
    AssociatedControlID="string"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    ControlToValidate="string"
    CssClass="string"
    Display="None|Static|Dynamic"
    EnableClientScript="True|False"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    ErrorMessage="string"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
                Large|X-Large|XX-Large"
        Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    runat="server"
    SetFocusOnError="True|False"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    Text="string"
    ToolTip="string"
    ValidationExpression="string"
    ValidationGroup="string"
    Visible="True|False"
    Width="size"
/>

备注:RegularExpressionValidator 控件用于确定输入控件的值是否与某个正则表达式所定义的模式相匹配。通过这种类型的验证,可以检查可预知的字符序列,如身份证号码、电子邮件地址、电话号码、邮政编码等中的字符序列。

注意:如果输入控件为空,则不调用任何验证函数且验证成功。使用 RequiredFieldValidator 控件防止用户跳过某个输入控件。

除非浏览器不支持客户端验证,或者显式禁用客户端验证(EnableClientScript 属性设置为 false),否则服务器端验证和客户端验证都要执行。

使用 ValidationExpression 属性指定用于验证输入控件的正则表达式。客户端的正则表达式验证语法和服务器端略有不同。在客户端,使用的是 JScript 正则表达式语法。而在服务器端使用的则是 Regex 语法。由于 JScript 正则表达式语法是 Regex 语法的子集,所以最好使用 JScript 正则表达式语法,以便在客户端和服务器端得到同样的结果。

示例

下面的代码示例演示如何使用 RegularExpressionValidator 控件来验证输入到文本框中的值是否为特定模式。在此例中,该模式为 5 位数字的邮政编码。验证结果随后显示在页上。

Visual Basic

<%@ 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 id="Form1" 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-Names="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 ID="Button1" 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 id="Form1" 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-Names="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 ID="Button1" text="Validate"
                    OnClick="ValidateBtn_Click"
                    runat="server"/>
            </td>
            <td></td>
         </tr>
      </table>
   </form>
</body>
</html>


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3