ASP.NET Tutorial/Validation/RegularExpressionValidator
Содержание
Making sure the text-box value is an e-mail address
<%@ 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>
Validate email
<%@ 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">
<div>
Email:<br />
<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" />
</div>
</form>
</body>
</html>
Validate Name
<%@ 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">
<div>
Name:<br />
<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" />
</div>
</form>
</body>
</html>
Validate phone number
<%@ 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">
<div>
Phone Number:<br />
<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" />
</div>
</form>
</body>
</html>
Validate URL
<%@ 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">
<div>
Web Site URL:<br />
<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" />
</div>
</form>
</body>
</html>
Validate Zip code
<%@ 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">
<div>
<asp:Label ID="Label3"
runat="server"
Text="The Following Field (TextBox3) Must be a US Zip Code."></asp:Label><br />
<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><br />
</form>
</body>
</html>