Use custom validation controls in ASP. NET

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.