ASP.Net/Validation by Function/Number

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

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

<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Checking the Range of a Number through the RangeValidator Control</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Enter a Number:<BR>
<asp:textbox 
    id="txtNumber" 
    runat=server 
/>
<asp:rangevalidator 
    id="rngQuantity" 
    controltovalidate="txtNumber" 
    type="Integer" 
    minimumvalue=5
    maximumvalue=44
    display="Dynamic"
    font-name="Verdana"
    font-bold="True"
    font-size="10pt"
    forecolor="blue"
    runat="server">
    The Quantity field must be from 5 to 44!
</asp:rangevalidator>
<BR><BR>
<asp:button 
    id="butOK"
    text="OK"
    type="Submit"
    runat="server"
/>
</form>
</BODY>
</HTML>



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

<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Comparing a Field to a Numeric Data Type using the CompareValidator Control</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Please enter a Whole Number:<BR>
<asp:textbox 
    id="txtNumber" 
    runat=server 
/>
<asp:comparevalidator 
    id="cvCheckNumber" 
    controltovalidate="txtNumber" 
    operator="DataTypeCheck" 
    type="Integer" 
    runat="server"
>
    You must enter a whole number!
</asp:CompareValidator><BR><BR>
<asp:button 
    id="butOK"
    text="OK"
    type="Submit"
    runat="server"
/>
</form>
</BODY>
</HTML>



Validating Against a Positive Even Whole Number using the CustomValidator Control (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Answer_ServerValidation(source As object, E As ServerValidateEventArgs)
    If  E.Value = CLng(E.Value) Then
        If E.Value Mod 2 = 0 Then
            If E.Value > 0 Then
                E.IsValid = True
            Else
                E.IsValid = False
            End If
        Else
            E.IsValid = False
        End If
    Else
        E.IsValid = False
    End If
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Validating Against a Positive Even Whole Number using the CustomValidator Control</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Enter a Positive, Even, Whole Number:<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>



Validation control:a postive whole number (VB.net)

<%@ 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 OK_OnSubmit(Sender As Object, E As EventArgs)
    If cvCheckNumber.IsValid Then
        ActiveForm = FinishForm
        lbl2.Text = "You entered " & txtNumber.Text
    End If
End Sub
</script>
<mobile:form 
    id="StartForm" 
    runat="server">
    <mobile:label 
        runat="server" 
        id="lbl1"
        Text="Enter a postive whole number:" 
    />
    <mobile:textbox
        runat="server" 
        id="txtNumber"
    />
    <mobile:comparevalidator 
        id="cvCheckNumber" 
        controltovalidate="txtNumber"
        valuetocompare=0
        operator="GreaterThan" 
        errormessage="You must enter a positive whole number!"
        type="Integer" 
        runat="server"
    />
    <mobile:Command 
        runat="server" 
        id="cmdOK"
        OnClick="OK_OnSubmit" 
        Text="OK" 
    />
</mobile:form>
<mobile:form 
    id="FinishForm" 
    runat="server">
    <mobile:label 
        runat="server" 
        id="lbl2"
    />
</mobile:form>