ASP.Net/Validation by Function/Zip Code

Материал из .Net Framework эксперт
Версия от 11:50, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Regular Expression Validator Example: zip code

<%@ Page %>
<html>
<body>
<h1>Regular Expression Validator Example</h1>
<form runat="server">
Zipcode:<asp:TextBox id="_zipcode" runat=server/><asp:RegularExpressionValidator
     ControlToValidate="_zipcode"
  Display="static"
  ErrorMessage="Zip code must be of the form 11111-1111"
  InitialValue="" width="100%" runat=server
  ValidationExpression="\d{5}(-\d{4})?">**
</asp:RegularExpressionValidator>
<br/>
<input value="Enter" type=submit />
</form>
</body> </html>



Using a RegularExpressionValidator Control to Test a Zip Code (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Answer_ServerValidation(source As object, E As ServerValidateEventArgs)
    If E.Value = "VB" or E.Value = "VB.NET" or E.Value = "C#" _
        or E.Value = "C#.NET" or E.Value = "A bunch of others." Then
        E.IsValid = True
    Else
        E.IsValid = False
    End If
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Using a RegularExpressionValidator Control to Test a Zip Code</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
What languages can you use with ASP.NET?<BR>
<asp:textbox 
    id="txtAnswer" 
    runat=server 
/>
<asp:customvalidator 
    id="custom9"
    controltovalidate="txtAnswer"
    OnServerValidate="Answer_ServerValidation"
    display="Dynamic"
    font-name="Verdana"
    font-bold="True"
    font-size="10pt"
    runat="server">
    <BR>Incorrect answer please try again!
</asp:CustomValidator>
<BR><BR>
<asp:button 
    id="butOK"
    text="OK"
    type="Submit"
    runat="server"
/>
</form>
</BODY>
</HTML>



Using a RegularExpressionValidator Control to Test a Zip Code with validationexpression (VB.net)

<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Using a RegularExpressionValidator Control to Test a Zip Code</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Please enter a 5-digit Zip Code:<BR>
<asp:textbox 
    id="txtZipCode" 
    runat=server 
/>
<asp:regularexpressionvalidator 
    id="regZipCode"
    controltovalidate="txtZipCode"
    validationexpression="^\d{5}$"
    display="Dynamic"
    font-name="Arial" 
    font-size="11"
    runat=server
>
    Zip code must be a number in the form of 12345.
</asp:regularexpressionvalidator>
<BR><BR>
<asp:button 
    id="butOK"
    text="OK"
    type="Submit"
    runat="server"
/>
</form>
</BODY>
</HTML>