MVC data verification and mvc data verification

Source: Internet
Author: User
Tags valid email address

MVC data verification and mvc data verification

In the Models folder of the application, add ---- new item --> ADO. NET Object Data Model --> model content --> next, select your database connection ". Here, the database I selected is sales. Next, you can select the table or view you want for "select database object". You can check the table or view you want, here I select the T_UserInfo table. After creating the instance, find salesModel. edmx.

After you click the triangle symbol in front of salesModel. edmx, The salesModel. edmx. Designer. cs file is displayed under salesModel. edmx. Open the file and you will see

One method: public partial class salesEntities: ObjectContext

SalesEntities is the ADO model entity class we created. (Note: Here I select the sales database. So the name of my object model class is salesEntities. Different database entity model classes you choose have different names. However, they all end with *** Entities)

MVC 3 data verification


ASP. net mvc 3 -- remote Model Verification

MVC4 data annotation and Verification

Controller Home

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. mvc; using MvcApplication2.Models; namespace MvcApplication2.Controllers {public class HomeController: Controller {// GET:/Home/public ActionResult Index () {return View ();} /// <summary> /// check whether the user name is repeated /// </summary> /// <param name = "userName"> the user is on the page (view) userName entered in the form </param> /// <returns> Json </r Eturns> public ActionResult CheckUserName (string userName) {bool result = false; using (var datas = new salesEntities () // create An ADO. NET Entity model. The object class model class name is salesEntities {var query = from userinfo in datas. t_UserInfo // query the T_UserInfo table where userinfo in the object model class using a linq statement. name = userName select new {Name = userinfo. name}; if (query = null |! Query. Any () {result = true ;}return Json (result, JsonRequestBehavior. AllowGet );}}}

Model (UserInfo)

Using System; using System. collections. generic; using System. linq; using System. web; using System. componentModel; using System. componentModel. dataAnnotations; using System. web. mvc; namespace MvcApplication2.Models {// <summary> // these examples are verified. Note that there is no client verification here, and they are all verified by the server. If an error occurs, the view will be rendered and displayed again, which is very easy to use and convenient. How many lines of code do you need to write before to handle these tedious and tasteless verifications? Microsoft is too compassionate to programmers. /// In the following example, when the data fails to be verified by the server, all the content entered by the user is cleared after the page is re-opened. If you do not want to clear the content, if the content you entered for the first time still exists, you can return View (accepted object); // </summary> public class UserInfo {public int Id {get; set ;} /// ID /// <summary> /// DisplayName: specify the name displayed in the current field // StringLength: Specify the allowed length. The first parameter in parentheses is the maximum length, the second parameter is the minimum length. The last parameter indicates that when the length of a single field value does not comply with the first and second parameters, the system prompts the error. // Required: indicates that the current field is Required, if this value is missing from the submitted form, a verification error is thrown. AllowEmptyStrings in parentheses is: gets or sets a value that indicates whether to allow null strings. When the single value is false, it indicates that the value of the current field cannot be blank // Remote: the client verification logic can be executed using the server-side callback function. To put it bluntly, AJAX verification is supported. The CheckUserName in the brackets is the Action method. Home is controller // </summary> [DisplayName ("username")] [Required (AllowEmptyStrings = false, ErrorMessage = "username cannot be blank")] [StringLength (10, minimumLength = 2, ErrorMessage = "username length must be between {2} and {1}")] [Remote ("CheckUserName", "Home ", errorMessage = "user name already exists")] public string UserName {get; set;} // user name [DisplayName ("User Password")] [DataType (DataType. password)] // display [Required (AllowEmptyStrings = false, ErrorMessage = "No Password Can be blank ")] [StringLength (12, MinimumLength = 6, ErrorMessage =" the password length must be between {2} and {1} ")] public string UserPassword {get; set;} // User Password [DataType (DataType. password)] [Compare ("UserPassword", ErrorMessage = "inconsistent passwords")] [DisplayName ("Confirm Password again")] public string TUserPassword {get; set ;} // output the password again /// <summary> // RegularExpression: the value of the current field must conform to a regular expression. If not, a verification error is returned. The content of ErrorMessage is the specific information of the verification error. /// </Summary> [Display (Name = "")] // You can also write it as [DisplayName ("")] [Required (AllowEmptyStrings = false, errorMessage = "email cannot be blank")] // [DataType (DataType. emailAddress, ErrorMessage = "xxxxxxxx")] // You can also use the following regular expression to verify the mailbox [RegularExpression (@ "[A-Za-z0-9. _ % +-] + @ [A-Za-z0-9] + \. [A-Za-z] {2, 4} ", ErrorMessage =" {0} format incorrect ")] public string Email {get; set ;} // Email [DisplayName ("Please confirm Email again")] [Compare ("Email", ErrorMessage = "Inconsistent email addresses entered twice")] // [DataType (DataType. emailAddress, ErrorMessage = "enter a valid Email Address")] public string TEmail {get; set;} // enter the email address [DisplayName ("ID card")] Again [RegularExpression (@ "\ d {17} [\ d | x] | \ d {15}", ErrorMessage = "Incorrect ID card number format")] public string IdentityNo {get; set ;}/// ID card No. /// <summary> /// Range: specifies the minimum and maximum values of the value type. "Minimum" of the first number in parentheses the second parameter is "maximum" // </summary> [DisplayName ("Age")] [Required (AllowEmptyStrings = false, errorMessage = "Age cannot be blank")] [Range (10,120, ErrorMessage = "the age you entered does not conform to the standard, and the age should be between {1}-{2}")] public int Age {get; set ;}/// Age /// <summary> /// DisplayFormat: Various formatting options for processing attributes. When a property contains a null value, you can provide optional display text, disable HTML encoding for the Property containing tags, and specify a formatted string for the property value at runtime. /// </Summary> [DisplayName ("amount")] [DataType (DataType. currency)] // Currency: indicates the Currency value [DisplayFormat (ApplyFormatInEditMode = true, DataFormatString = "{0: c}")] [Required (ErrorMessage = "the amount cannot be blank")] [Range (typeof (decimal), "20.0", "30.0 ", errorMessage = "amount between {1} and {2}")] public decimal Money {get; set;} // amount [Display (Name = "mobile phone number")] [DataType (DataType. phoneNumber)] [RegularExpression (@ "(\ d {11}) | ^ (\ d {7, 8}) | (\ d {4} | \ d {3 }) -(\ d {7, 8}) | (\ d {4} | \ d {3})-(\ d {7, 8 }) -(\ d {4} | \ d {3} | \ d {2} | \ d {1}) | (\ d {7, 8 }) -(\ d {4} | \ d {3} | \ d {2} | \ d {1}) $) ", ErrorMessage =" Incorrect format ")] public int Phome {get; set;} [Display (Name = "")] // [DisplayFormat (ApplyFormatInEditMode = true, DataFormatString = "yyyy/MM/dd")] [Required] // the value of the current field cannot be blank public DateTime Birthday {get; set;} // Birthday [Display (Name = "Remarks")] [DataType (DataType. multilineText)] // The current field is a multiline text public string Remarks {get; set ;}// Remarks }}


View (Index)

@model MvcApplication2.Models.UserInfo@{    Layout = null;}<!DOCTYPE html>



Zookeeper
How do you implement mvc data verification?

System. ComponentModel. DataAnnotations use this verification
 
Mvc data verification

MVC has its own independent verification system. Simple verification does not need to be written into JS. For details, see www.cnblogs.com/..2.html. you can add restrictions to the above class. If you have passed the database (such as verifying whether the user name has been registered ), you can use the Ajax callback function to process
 

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.