|
There are many verification controls in Asp.net 1.1, which greatly facilitates us. However, sometimes, when special verification is required, we may feel that they are not enough, therefore, you can use the custom verification control customvalidator. To use this control, you must write the corresponding event on the server. The format is as follows:
|
Sub functionname (sender as object, argS as servervalidateeventargs)
...
End sub |
Note that two parameter values indicate which control is verified by customvalidator. If isvalid is true, the page control has passed verification. Here is an example of verifying whether the input in the text box on the page is a prime number:
|
<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>
|
As you can see, in the verification control, onservervalidate = "primenumbercheck", You need to define a specific onservervalidate event. Then, you must return ARGs in the specific event processing process. isvalid value, which indicates whether the verification is successful by price. Of course, the page is used at last. isvalid attribute for full verification.
|