RegularExpressionValidator.ValidationExpression Proprietà

Definizione

Ottiene o imposta l'espressione regolare che determina il criterio utilizzato per convalidare un campo.

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

Valore della proprietà

Stringa che specifica l'espressione regolare utilizzata per convalidare il formato di un campo. Il valore predefinito è Empty.

Attributi

Eccezioni

Il formato dell'espressione regolare non è corretto.

Esempio

Nell'esempio seguente viene illustrato come usare la ValidationExpression proprietà per convalidare un campo con cinque cifre numeriche. Quando si fa clic sul controllo, il gestore eventi risultante OnClick controlla la IsValid proprietà dell'oggetto Page per determinare se il Button testo nell'oggetto TextBox soddisfa l'espressione regolare.

Importante

L'esempio include una casella di testo che accetta l'input dell'utente e rappresenta quindi una potenziale minaccia alla sicurezza. Per impostazione predefinita, le pagine Web ASP.NET verificano che l'input dell'utente non includa script o elementi HTML. Per altre informazioni, vedere Cenni preliminari sugli attacchi tramite script.

<%@ 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>

Commenti

Utilizzare questa proprietà per specificare il modello usato per verificare le sequenze prevedibili di caratteri, ad esempio quelle presenti in numeri di sicurezza sociale, indirizzi di posta elettronica, numeri di telefono e codici postali.

RegularExpressionValidator non esegue la convalida su una stringa vuota. Se la stringa di cui si esegue il test potrebbe essere vuota, usare l'oggetto RequiredFieldValidator e .RegularExpressionValidator

Per altre informazioni sulle espressioni regolari, vedere Espressioni regolari di .NET Framework.

Nota

Se si verificano problemi con costrutti di corrispondenza dei modelli, provare a eseguire il wrapping dell'espressione con "^(" e ")$". Ad esempio, "a|ab" diventa "^(a|ab)$".

Questa proprietà non può essere impostata da temi oppure temi di fogli di stile. Per altre informazioni, vedere ThemeableAttribute e ASP.NET Temi e skin.

Si applica a