MVC4 data Annotations and validation

Source: Internet
Author: User

validation annotation features in model:

PublicClass Stuinfo {Publicint ID {GetSet;} [Display (Name ="Name")]Set the name of the field to display [Required (errormessage ="You need to fill in {0}")]Set as must field has been incorrectly prompted [Stringlength (Minimumlength =3)]Set maximum length and minimum lengthPublicString Name {GetSet;} [Display (Name ="Age")] [Range (1,ErrorMessage ="Age is not correct!" ")]Setting the range of valuesPublicint Age {GetSet;} [Display (Name ="Height")] [Range (typeofdecimal),"50.00","250.00", errormessage ="Height exceeds specified range")]PublicDecimal Height {GetSet;} [Display (Name ="Birthday")] [DataType (datatype.date,errormessage ="{0} format is incorrect")]Setting data types and error hintsPublic DateTime Birthday {GetSet;} [Display (Name ="Phone")] [Remote ("Checkphone","Stuinfo", errormessage ="{0} has been registered")]Validates the data in the channel (route) in the specified ConterollerPublicString Phone {GetSet;} [Display (Name ="Address")] [DataType (Datatype.multilinetext)]PublicString Address {GetSet;} [Display (Name ="E-mail")] [RegularExpression (@ "(\w) + (\.\w+) *@ (\w) + ((\.\w+) +)", errormessage ="{0} format is incorrect")]Regular validationPublicstring Email {get; set;} [Display (Name = //settings compare two field values public  String Emailconfirm {get; set;} [Display (Name =  "password")] [DataType (Datatype.password)] public string Password {get; set;} [Display (Name =  "{0} format is incorrect")] public string email_b {get; set;}              

Channels for Remote authentication:

Public Jsonresult Checkphone (string phone)              {                0;                return Json (result, jsonrequestbehavior.allowget);          }  

Effect:
The normal page:

The field validation effect is required:

Value range, value type validation effect:

Regular validation and comparison of field value effects:

Data type validation effect:

MVC4 data Annotations and validation

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.