. Net validator verification framework [. Net | validator framework | attribute]

Source: Internet
Author: User

 

Preface

Ease of use is the goal of the. NET validator verification framework. Based on the attribute validators identification and rich verification return types, it provides scalable validators interface... -- another wish and idea start, happy ing :)

 

Body

I,Framework Introduction

This framework matches the property name, the key in form and querystring, And the ID of the server-side control based on the attribute validator of the property, verify the data according to the validators.

Validators Structure

II,Framework example

2.1 regular verification

2.1.1 prepare the model class user. Pay attention to reference the namespace using lasy. validator;

Public sealed class user
{
# Region member variable

Private string username;
Private string email;
Private int userpermission;

# Endregion

# Region Properties

/// <Summary>
/// User Name
/// </Summary>
[Required]
Public String Username
{
Get {return username ;}
Set {username = value ;}
}
/// <Summary>
/// Email
/// </Summary>
[Email]
Public String email
{
Get {return email ;}
Set {email = value ;}
}

/// <Summary>
/// User Permissions
/// </Summary>
[Intrange (1, 5)]
Public int userpermission
{
Get {return userpermission ;}
Set {userpermission = value ;}
}

# Endregion

}

2.1.2 console Test

Static void main (string [] ARGs)
{

Idictionary <string, Object> dict = new dictionary <string, Object> ();
Dict. Add ("username", "over ");
Dict. Add ("email", "over140gmail.com ");
Dict. Add ("userpermission", "5 ");

Validator = new validator (typeof (User ));

Console. Write ("validator Result :");
Console. writeline (validator. Validate (dict). errorstringresult );

Dict ["email"] = "over140@gmail.com ";

Console. Write ("validator Result :");
Console. writeline (validator. Validate (dict). boolresult );

Console. Readline ();
}

2.1.3 Result Display

2.2 Form Verification

2.2.1 user class

Class user
{
# Region member variable

Private string username;

Private string password;

# Endregion

# Region Properties

/// <Summary>
/// User Name
/// </Summary>
[Required]
[Minlength (5, failkeydesc = "username")]
Public String Username
{
Get {return username ;}
Set {username = value ;}
}

/// <Summary>
/// Password
/// </Summary>
[Required]
Public String Password
{
Get {return password ;}
Set {Password = value ;}
}

# Endregion
}

2.2.2 test page

Login. aspx

<Form ID = "form1" runat = "server">
User name:
<Asp: textbox id = "username" runat = "server"> </ASP: textbox>
<Br/>
Password:
<Asp: textbox id = "password" runat = "server"> </ASP: textbox>
<Br/>
<Asp: button runat = "server" text = "Logon Verification" onclick = "unnamed1_click"/>
</Form>

Button code

Protected void unnamed1_click (Object sender, eventargs E)
{
// Obtain all elements of the form. This. controls [3]. Controls
If (New validator (typeof (User). Validate (this. username, this. Password). boolresult)
{
Response. Write ("Verification Successful! ");
}
Else
{
Response. Write ("Verification Failed! ");
}
}

2.2.3 Effect

 

III,Suggestions

3.1 If the client verification has been performed and the server extracts form data from the request for operations, you can directly use the validator (type, bool checkall, bool setdefaultvalue) constructor, then, obtain the idictionary <string, Object> data that has been verified from the valuesresult.

3.2 novalidator and valuesresult are used together when extracting form data.

3.3 During debugging, note that the attribute of the class is cached statically. You need to clear the attribute or regenerate the modified validator ID!

 

IV,Download

4.1 source code SRC

4.2 file lasy. validator. CHM

 

Conclusion

Although wheel building is often a thankless thing, I think it will be much better to recommend it to my friends if I think it is useful or want to make something useful, besides, many enthusiastic people provide valuable suggestions :)

 

 

Other articles you may be interested in

 

1. NOD32 upgrade account Updater [C # | NOD32 | ESET]

2. Use Visual Studio 2005/2008 to extract resources from the EXE file [Image | HTM | cursor file]

3. Sina flash download tool [httpwebrequest | Sina Video]

4. Stupid image batch download tool v0.3 BETA [C # | winform | Regular Expression | httpwebrequest | async asynchronous programming] New

 

 

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.