Condividi tramite


Classe RegularExpressionValidator

Rende disponibile per un controllo la funzionalità con cui verificare se il valore di un altro controllo corrisponde all'espressione regolare specificata.

public class System.Web.UI.MobileControls.RegularExpressionValidator : 
   System.Web.UI.MobileControls.BaseValidator

Osservazioni

La classe si comporta in modo identico al controllo ASP .NET System.Web.UI.WebControls.RegularExpressionValidator.

Per impostazione predefinita, i controlli Command di un form generano controlli di convalida nel form per eseguire la convalida quando il form viene inviato al server. Per disattivare la convalida automatica, impostare su false la proprietà CausesValidation dei controlli Command.

Esempio

Nell'esempio che segue viene illustrato come aggiungere le proprietà di espressioni regolari, ad esempio le proprietà ValidationExpression e Text, a livello di codice durante il caricamento di una pagina.

<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" 
   Language="VB" %>
<%@ Register TagPrefix="mobile"
    Namespace="System.Web.UI.MobileControls"
    Assembly="System.Web.Mobile" %>

<script runat="server" language="vb">

Sub Page_Load(sender As Object, e As EventArgs)
   ' Define validation expressions.
   revUserName.ValidationExpression = "^[a-zA-Z](.{1,9})$"
   revPhone.ValidationExpression = _
      "((\\(\\d{3}\\) ?)|(\\d{3}-))?\\d{3}-\\d{4}"

   rfvUserName.Text = "User name is required"
   revUserName.Text = "Must be between 2 to 10 characters long"
   revPhone.Text = "Please provide a valid number: (425) 555-0187"

   ' ErrorMessages appear in ValidationSummary.
   revUserName.ErrorMessage = "Incorrect UserName format. Name" + _ 
                              " can be 2 to 10 characters long"
   rfvUserName.ErrorMessage = "User name required"
   revPhone.ErrorMessage = "Please provide a valid number: (425) 555-0187"
End Sub

Sub OnCmdClick1(sender As Object, e As EventArgs)
   If Page.IsValid Then
      ActiveForm = Form2
   End If
End Sub
</script>

<mobile:Form runat="server" id="Form1" >
   <mobile:Label runat="server" id="lblHeading" 
      Text="Provide your name and number" StyleReference="title" />   
   <mobile:Label runat="server" id="lblUserName" 
      Text="User Name (req'd)" />
   <mobile:Textbox  runat="server" id="txtUserName"/>
   <mobile:RequiredFieldValidator runat="server" id="rfvUserName" 
      ControlToValidate="txtUserName" />
   <mobile:RegularExpressionValidator runat="server" id="revUserName" 
      ControlToValidate="txtUserName" />
   <mobile:Label runat="server" id="lblPhone" Text="Phone" />
   <mobile:Textbox  runat="server" id="txtPhone"/>
   <mobile:RegularExpressionValidator runat="server" id="revPhone" 
      ControlToValidate="txtPhone" />
   <mobile:ValidationSummary FormToValidate="Form1"
      HeaderText="Error Summary:"
      runat="server" />
   <mobile:Command runat="server" id="Command1" text="Submit" 
      OnClick="OnCmdClick1"/>
</mobile:Form>

<mobile:Form runat="server" id="Form2" >
   <mobile:Label runat="server" Text="Thank You." />
</mobile:Form>
[C#]
<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" 
   Language="c#" %>
<%@ Register TagPrefix="mobile"
    Namespace="System.Web.UI.MobileControls"
    Assembly="System.Web.Mobile" %>

<script runat="server" language="c#">

void Page_Load(Object sender, EventArgs e)
{
   // Define validation expressions.
   revUserName.ValidationExpression = "^[a-zA-Z](.{1,9})$";
   revPhone.ValidationExpression = 
      "((\\(\\d{3}\\) ?)|(\\d{3}-))?\\d{3}-\\d{4}";

   rfvUserName.Text = "User name is required";
   revUserName.Text = "Must be between 2 to 10 characters long";
   revPhone.Text = "Please provide a valid number: (425) 555-0187";

   // ErrorMessages appear in ValidationSummary.
   revUserName.ErrorMessage = "Incorrect UserName format. Name" + 
                              " can be 2 to 10 characters long";
   rfvUserName.ErrorMessage = "User name required";
   revPhone.ErrorMessage = "Please provide a valid number: (425) 555-0187";
}

void OnCmdClick1(Object sender, EventArgs e)
{
   if (Page.IsValid)
   {
      ActiveForm = Form2;
   }
}
</script>

<mobile:Form runat="server" id="Form1" >
   <mobile:Label runat="server" id="lblHeading" 
      Text="Provide your name and number" StyleReference="title" />   
   <mobile:Label runat="server" id="lblUserName" 
      Text="User Name (req'd)" />
   <mobile:Textbox  runat="server" id="txtUserName"/>
   <mobile:RequiredFieldValidator runat="server" id="rfvUserName" 
      ControlToValidate="txtUserName" />
   <mobile:RegularExpressionValidator runat="server" id="revUserName" 
      ControlToValidate="txtUserName" />
   <mobile:Label runat="server" id="lblPhone" Text="Phone" />
   <mobile:Textbox  runat="server" id="txtPhone"/>
   <mobile:RegularExpressionValidator runat="server" id="revPhone" 
      ControlToValidate="txtPhone" />
   <mobile:ValidationSummary FormToValidate="Form1"
      HeaderText="Error Summary:"
      runat="server" />
   <mobile:Command runat="server" id="Command1" text="Submit" 
      OnClick="OnCmdClick1"/>
</mobile:Form>

<mobile:Form runat="server" id="Form2" >
   <mobile:Label runat="server" Text="Thank You." />
</mobile:Form>

Requisiti

Spazio dei nomi: System.Web.UI.MobileControls

Assembly: System.Web.Mobile

Vedere anche

Controllo RegularExpressionValidator | Classe BaseValidator | Classe CompareValidator | Classe CustomValidator | Classe RangeValidator | Classe RequiredFieldValidator | Classe ValidationSummary