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: