ASP.Net/Validation by Function/Date

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

Checking the Range of a Date through the RangeValidator Control (VB.net)

   <source lang="csharp">

<%@ Page Language=VB Debug=true %> <HTML> <HEAD> <TITLE>Checking the Range of a Date through the RangeValidator Control</TITLE> </HEAD> <form runat="server">

Enter a Date:
<asp:textbox

   id="txtDate" 
   runat=server 

/> <asp:RangeValidator

   id="rngDate" 
   ControlToValidate="txtDate" 
   type="Date" 
   minimumvalue="1/1/1990"
   maximumvalue="12/31/1999"
   display="Dynamic"
   runat="server">
   The Date field must be from 1/1/1990 to 12/31/1999!

</asp:RangeValidator>

<asp:button

   id="butOK"
   text="OK"
   type="Submit"
   runat="server"

/> </form> </BODY> </HTML>

      </source>
   
  


Comparing a Field to a Date Data Type using the CompareValidator Control (VB.net)

   <source lang="csharp">

<%@ Page Language=VB Debug=true %> <HTML> <HEAD> <TITLE>Comparing a Field to a Date Data Type using the CompareValidator Control</TITLE> </HEAD> <form runat="server">

Please enter a Date:
<asp:textbox

   id="txtDate" 
   runat=server 

/> <asp:comparevalidator

   id="cvCheckDate" 
   controltovalidate="txtDate" 
   operator="DataTypeCheck" 
   type="Date" 
   runat="server"

>

   You must enter a date!

</asp:CompareValidator>

<asp:button

   id="butOK"
   text="OK"
   type="Submit"
   runat="server"

/> </form> </BODY> </HTML>

      </source>
   
  


Validating for a Date in the Current Year Using the CustomValidator Control (VB.net)

   <source lang="csharp">

<%@ Page Language=VB Debug=true %> <script runat=server> Sub Date_ServerValidation(source As object, E As ServerValidateEventArgs)

   If  IsDate(E.Value) Then
       If  Year(E.Value) = Year(Today) Then
           E.IsValid = True
       Else
           E.IsValid = False
       End If
   Else
       E.IsValid = False
   End If

End Sub Sub SubmitBtn_Click(Sender As Object, E As EventArgs) End Sub </SCRIPT> <HTML> <HEAD> <TITLE>Validating for a Date in the Current Year Using the CustomValidator Control</TITLE> </HEAD> <form runat="server">

Enter a date in the current year:
<asp:textbox

   id="txtDate" 
   runat=server 

/> <asp:customvalidator

   id="customDate"
   controltovalidate="txtDate"
   onservervalidate="Date_ServerValidation"
   display="Dynamic"
   font-name="Verdana"
   font-bold="True"
   font-size="10pt"
   runat="server">
   You must enter a date in the current year!

</asp:CustomValidator>

<asp:button

   id="butOK"
   text="OK"
   type="Submit"
   onclick="SubmitBtn_Click" 
   runat="server"

/> </form> </BODY> </HTML>

      </source>