Custom verification class (both client and server verification, supporting Ajax verification) [Open Source]

Source: Internet
Author: User

Recently, I was working on a small project and used a user registration. due to security concerns, the client and server are required to be verified at the same time. I used Microsoft's verification control in the project, I found it was really troublesome to use it. I dragged more than a dozen verification controls on the page, not to mention it, because the custom error information is displayed when the verification fails, which is quite troublesome to do, find some minor changesCodeThey are not convenient. The most important thing is that they cannot be used repeatedly. So I want to make a verification control myself. I originally wanted to do a custom control. Later I found that there are many verification definitions, finally, select the class library. The page call class is used to verify the control. Well, I will not talk much about it. Next, let's take a look at it. For the first time, open source. Please give us more advice.

Let's take a look at the code called on the page.

Page call
Using System;
Using System. collections;
Using System. configuration;
Using System. Data;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;

Namespace Demo
{
Public   Partial   Class Webform1: system. Web. UI. Page
{
Koodoo. validation. formvalidate FV;
Protected   Void Page_load ( Object Sender, eventargs E)
{
FV =   New Koodoo. validation. formvalidate ( This , " Button1 " , True );
FV. valiajaxemail ( " Useremail " , " Enter an email in the correct format. " , " The email you entered already exists. " , " Checkemail. aspx " , " Key " );
FV. valinormalnumber ( " Textbox1 " , " Enter a number ranging from 2 to 6. " , False , 2 , 6 );
FV. valicompare ( " Textbox2 " , " The previous number is not equal. " , " Textbox1 " );
FV. valicheckbox ( " Checkboxlist1 " , " Select at least two " , 2 );
FV. valiradio ( " Radiobuttonlist1 " , " Select " );
FV. valiselect ( " Dropdownlist1 " , " Select " , " Select " );
FV. Display ();
}

Protected   Void Button#click ( Object Sender, eventargs E)
{
If (FV. submit_server ())
{
Response. Write ( " Verification passed " );
}
Else
{
Response. Write ( " Verification Failed " );
}
}
}
}

Instantiation 1-> parameter 1: Current page, parameter 2: Submitted button, parameter 3: whether it is in development stage (when it is in development stage, every time JavaScript code is generated, it is placed under the JS folder of the website. If it is in the running stage, it is set to false)

FV = new koodoo. validation. formvalidate (this, "button1", true );

Instantiation 2-> parameter 1: Current page, parameter 2: submit button, parameter 3: Display error message mode (1: Each control has a correct label Display error, 2: Display All error messages on a specified label, 3: display an error message on a specified label) parameter 4: display the specified label of the error message, valid when parameter 3 is 2 and 3. Parameter 5: whether it is in development stage

FV = new koodoo. validation. formvalidate (this, "buttin1", 1, "errmsg", true );

After use, call display () to add JS Code to the page.

Submit_server () is called for server-side verification. If true is returned, the verification succeeds.

When an error is reported through verification, you can customize the JS script to display some custom error reports on the page.

As I used here: (Note: when the error reporting method is 1, the ID of the label that shows the error is, and the verified control is added with "_ Err ", if "_ x" is added when "required" is added, it is not clear. Check the source code)

VaR showerr = function (ID)

{
$ ("#" + Id). ATTR ("style", "border: 1px # ff0000 solid ;");
$ ("#" + ID + "_ Err"). ATTR ("style", "color: Red ");
$ ("#" + ID + "_ x"). ATTR ("style", "color: Red ");
}
VaR hideerr = function (ID ){
$ ("#" + Id). ATTR ("style ","");
$ ("#" + ID + "_ Err"). ATTR ("style ","");
$ ("#" + ID + "_ x"). ATTR ("style ","");
}

For more information, see the source code.

Source code updatedSource code download

I sent it to the homepage for the first time, waiting for you to make a picture.

Related Article

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.