Introduction to ASP. NET MVC common built-in validation features

Source: Internet
Author: User

0.[displayname ("name")]

0. [DisplayName (" name ")]

1.[required]: Must be entered

" Please enter user name ")]  

2.[stringlength]: Limit string length

[Stringlength ( length cannot exceed 10 characters ")]  

3.[range]: Limit the range of values

1 [range (0, errormessage = " age range between 0-120 years old ")]

  

4.[regularexpression]: Must conform to a regular expression (1) to write an expression directly using RegularExpression:

1 [RegularExpression (@ "^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$"" Please enter email format ")]  2 Public Getset; }  

(2) Custom features:

1  Public class Emailattribute:regularexpressionattribute   2     {  3public           emailattribute ()  4             :  Base(@ "^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$")  5        {  6        }   7     }  



Comparison of 5.Compare Judgments

 Public string Get Set ; }    [Compare ("email"" email ")]    Public string Get set; }  

6.remote:ajax Remote Authentication, the return value is bool type TRUE indicates validation passed

//action method Controller class request mode[Remote ("User","Validate", HttpMethod ="Post", errormessage ="user name already exists")]   Public stringUserName {Get;Set; } //....   PublicActionResult User () {//..   if(true)    {      returnJson (true, Jsonrequestbehavior.allowget); }    Else      returnJson (false, Jsonrequestbehavior.allowget); }  

7.OutputCache Page Caching

[OutputCache (duration=)] // Set page Absolute cache  Cache Time is 20 sec   public   actionresult Index ()  {       //  Verbose Code   }  

8. Hide Fields

[Hiddeninput (displayvalue=false)]  Public string Get set; }

Introduction to common built-in validation features for ASP. NET MVC

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.