Mvc4 data annotation and Verification

Source: Internet
Author: User

In traditional Asp.net development, data verification has always been a hot topic, whether it is client verification or server verification, we usually write complicated verification methods for a simple business. In MVC, MVC provides us with various data annotation and data verification features, which makes it easy for us to implement data verification, the following are my common data annotations and verification dome! Note: Custom verification features are not written in this article!

 

Verification annotation features in model:

 

Public class stuinfo {public int ID {Get; set;} [display (name = "name")] // set the field name [required (errormessage = "you need to enter {0}")] // if the required field already has an error message [stringlength (50, minimumlength = 3)] // set the maximum length and minimum length public string name {Get; set;} [display (name = "Age")] [range (1,150, errormessage = "Incorrect age! ")] // Set the value range to public int age {Get; set;} [display (name =" height ")] [range (typeof (decimal)," 50.00 ", "250.00", errormessage = "height beyond the specified range")] public decimal height {Get; set;} [display (name = "Birthday")] [datatype (datatype. date, errormessage = "{0} incorrect format")] // set the data type and error message public datetime birthday {Get; set ;} [display (name = "phone")] [Remote ("checkphone", "stuinfo", errormessage = "{0} registered")] // verify the Public String phone {Get; set;} [display (name = "Address")] [datatype (datatype. multilinetext)] Public String address {Get; set;} [display (name = "email")] [regularexpression (@ "(\ W) + (\. \ W +) * @ (\ W) + ((\. \ W +) ", errormessage =" {0} incorrect format ")] // regular verification Public String email {Get; set ;} [display (name = "enter email again")] [compare ("email", errormessage = "{0} two input inconsistencies")] // set and compare the values of the two fields Public String emailconfirm {Get; set;} [display (name = "password")] [datatype (datatype. password)] Public String password {Get; set;} [display (name = "backup email")] [datatype (datatype. emailaddress, errormessage = "{0} incorrect format")] Public String email_ B {Get; Set ;}}

Remote verification channel:

 
Public jsonresult checkphone (string phone) {var result = stuinfobll. findphone (phone). Count = 0; return JSON (result, jsonrequestbehavior. allowget );}

Effect:
Normal webpage:

 

 

 

 

 

 

 

 

Required field verification results:

 

Value range and value type verification results:

 

Regular Expression verification and comparison of field values:

 

Data Type verification results:

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.