<script language="vb" runat="server"> Sub btnSubmit_Click(sender as Object, e as EventArgs) If Page.IsValid then Response.Write("<font color=""red""><i>" & txtPrimeNumber.Text & _ " is, indeed, a good prime number.</i></font>") Else Response.Write("<font color=""red""><i>" & txtPrimeNumber.Text & _ " is <b>not</b> a prime number.</i></font>") End If End Sub
Sub PrimeNumberCheck(sender as Object, args as ServerValidateEventArgs) Dim iPrime as Integer = Cint(args.Value), iLoop as Integer, _ iSqrt as Integer = CInt(Math.Sqrt(iPrime)) For iLoop = 2 to iSqrt If iPrime mod iLoop = 0 then args.IsValid = False Exit Sub End If Next args.IsValid = True End Sub </script> <form method="post" runat="server"> Enter your favorite prime number: <asp:textbox id="txtPrimeNumber" runat="server" /> <%-- Create the CustomValidator control --%> <asp:CustomValidator runat="server" id="custPrimeCheck" ControlToValidate="txtPrimeNumber" OnServerValidate="PrimeNumberCheck" ErrorMessage="Invalid Prime Number" /> <%-- Create two CompareValidator controls: the first ensures that the number entered by the user is an Integer; the second makes sure it is positive. --%> <asp:CompareValidator runat="server" id="compPrimeNumber" Operator="DataTypeCheck" Type="Integer" Display="Dynamic" ControlToValidate="txtPrimeNumber" ErrorMessage = "You must enter an integer value." /> <asp:CompareValidator runat="server" id="compPrimeNumberPositive" Operator="GreaterThan" Type="Integer" Display="Dynamic" ValueToCompare="0" ControlToValidate="txtPrimeNumber" ErrorMessage = "You must enter a value greater than zero." /> <p><asp:button id="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" /> </form>
|