ASP.NET Tutorial/Validation/RegularExpressionValidator

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Making sure the text-box value is an e-mail address

   <source lang="csharp">

<%@ Page Language="VB" %> <script runat="server">

   Protected Sub Button1_Click(sender As Object, e As EventArgs)
      Label1.Text = "Passwords match"
   End Sub

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>CompareFieldValidator</title>

</head> <body>

   <form id="Form1" runat="server">
       Email:
       <asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox>
       <asp:RegularExpressionValidator ID="RegularExpressionValidator1"
                                       Runat="server" 
                                       ControlToValidate="TextBox1"
                                       ErrorMessage="You must enter an email address"
                                       ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
       </asp:RegularExpressionValidator>
       
           <asp:Button ID="Button1" OnClick="Button1_Click" 
            Runat="server" Text="Login"></asp:Button>
       
       
           <asp:Label ID="Label1" Runat="server"></asp:Label>
       
   </form>

</body> </html></source>


Validate email

   <source lang="csharp">

<%@ Page Language="C#" %> <!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 runat="server">

   <title>Regular Expression Tester</title>

</head> <body>

   <form id="form1" runat="server">
       Email:
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="regEmail" runat="server" ControlToValidate="txtEmail" ValidationExpression="^(.+)@([^\.].*)\.([a-z]{2,})$" Text="Enter a valid email" /> <asp:Button ID="btnSubmit" Text="Click this to test validation" runat="server" />
   </form>

</body> </html></source>


Validate Name

   <source lang="csharp">

<%@ Page Language="C#" %> <!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 runat="server">

   <title>Regular Expression Tester</title>

</head> <body>

   <form id="form1" runat="server">
   Name:
<asp:TextBox ID="txtName" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="regName" runat="server" ControlToValidate="txtName" ValidationExpression="^[a-zA-Z".\s]{1,50}" Text="Enter a valid name" /> <asp:Button ID="btnSubmit" Text="Click this to test validation" runat="server" />
   </form>

</body> </html></source>


Validate phone number

   <source lang="csharp">

<%@ Page Language="C#" %> <!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 runat="server">

   <title>Regular Expression Tester</title>

</head> <body>

   <form id="form1" runat="server">
       Phone Number:
<asp:TextBox ID="txtPhone" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="regPhone" runat="server" ControlToValidate="txtPhone" ValidationExpression="^(\(?\s*\d{3}\s*[\)\-\.]?\s*)?[2-9]\d{2}\s*[\-\.]\s*\d{4}$" Text="Enter a valid phone number" /> <asp:Button ID="btnSubmit" Text="Click this to test validation" runat="server" />
   </form>

</body> </html></source>


Validate URL

   <source lang="csharp">

<%@ Page Language="C#" %> <!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 runat="server">

   <title>Regular Expression Tester</title>

</head> <body>

   <form id="form1" runat="server">
       Web Site URL:
<asp:TextBox ID="txtUrl" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="regUrl" runat="server" ControlToValidate="txtUrl" ValidationExpression="^((http|https)://)?([\w-]+\.)+[\w]+(/[\w- ./?]*)?$" Text="Enter a valid URL" /> <asp:Button ID="btnSubmit" Text="Click this to test validation" runat="server" />
   </form>

</body> </html></source>


Validate Zip code

   <source lang="csharp">

<%@ Page Language="C#" %> <!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 runat="server">

   <title>Regular Expression Tester</title>

</head> <body>

   <form id="form1" runat="server">
       <asp:Label ID="Label3" 
                  runat="server" 
                  Text="The Following Field (TextBox3) Must be a US Zip Code."></asp:Label>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox3" ErrorMessage="TextBox3 Must be a US Zip Code" SetFocusOnError="True" ValidationExpression="\d{5}(-\d{4})?">*</asp:RegularExpressionValidator>
</form>

</body>

</html></source>