Asp.net mvc3 data verification (1)

Source: Internet
Author: User
For Web developers, it is an important but tedious task to verify user input information, and many developers will ignore it. The Asp.net mvc3 framework uses the dataannotations method for data verification. This method allows the program to perform dual verification on the client and server (Asp.net's data verification control is also dual verification ). The advantage of Dual-validation is that client verification will directly correspond to users without submitting forms, which reduces the pressure on the server and improves the user experience. server-side Verification ensures Data Validity and integrity, because sometimes the client disables the script function. Note: To implement client verification, you must reference the jquery and jquery verification files:
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script><script src="@Url.Conten("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
To implement server-side verification, run modelstate. isvalid after submitting the form. I. Basic Verification 1. Require (non-empty verification)Model:
[Required] [display (name = "username")] Public String username {Get; set ;}
Verification Result:

2. stringlength (String Length verification)You can verify the maximum length and minimum length of the model attribute, which correspond to maximumlength and minimumlength respectively. minimumlength is optional. Model:
[Required] [stringlength (100, minimumlength = 6)] [datatype (datatype. Password)] [display (name = "password")] Public String password {Get; set ;}
Verification Result

3. regularexpression (regular expression)Verify the attributes that match the regular expression. Model:
[Required] [datatype (datatype. emailaddress)] [regularexpression (@ "[A-Za-z0-9. _ % +-] + @ [A-Za-z0-9. _] + \. [A-Za-Z] {2, 4} ")] [display (name =" email address ")] Public String email {Get; set ;}
Verification Result:

  4. Range (Numerical range verification)This parameter is used to specify the maximum and minimum values of a value. The first parameter is the minimum value, and the second parameter is the maximum value (including them ). The int and double types are supported. Model:
[Required] [range (13, 18)] [display (name = "Age")] public int age {Get; set ;}
Verification Result:

Ii. Additional verificationAdditional verification is an additional two verification features added in system. Web. MVC. 1. Remote (remote verification)Although Asp.net mvc3 provides many features for directly verifying data in the model, many complicated logic verification cannot be performed in the model. Therefore, the mvc3 framework provides this remote verification feature, it allows developers to use C # code in the Controller to verify the data validity. Verify that the user name is repeated in a classic application. This data verification cannot be performed on the client unless all user names are sent to the client (which is obviously not possible ). Therefore, the remote feature only performs server-side verification. However, it is verified asynchronously, so it has a better user experience. Model:
[Required] [display (name = "username")] [Remote ("checkusername", "account")] Public String username {Get; set ;}
Controller:
        public JsonResult CheckUserName(string userName)        {            var result = userName == "admin";            return Json(result, JsonRequestBehavior.AllowGet);        }
Verification Result: Note: If you want to submit a POST request, you need to add httpmethod = "Post ":

[Required (errormessageresourcetype = typeof (errormessage), errormessageresourcename = "userrequire")] [display (name = "username")] [Remote ("checkusername", "Account ", httpmethod = "Post")] Public String username {Get; set ;}

The controller can also directly accept POST requests. Of course, you can add [httppost:

        //[HttpPost]        public JsonResult CheckUserName(string userName)        {            var result = userName != "admin";            return Json(result, JsonRequestBehavior.AllowGet);        }
  2. Compare (same verification)The compare feature is used to verify whether the two input data are identical. The most typical example is whether the password entered twice at registration is the same. Model:
[Required] [stringlength (100, errormessage = "{0} must contain at least {2} characters. ", Minimumlength = 6)] [datatype (datatype. password)] [display (name = "password")] Public String password {Get; set;} [ype (datatype. password)] [display (name = "Confirm Password")] [compare ("password", errormessage = "the password does not match the Confirm Password. ")] Public String confirmpassword {Get; set ;}
Verification Result:

The above are several common data annotations in the Asp.net mvc3 framework. Apart from the latter two MVC features, other features are in the system. componentmodel. in dataannotations, proper use will greatly improve the development efficiency. See http://msdn.microsoft.com/zh-cn/library/system.componentmodel.dataannotations.aspx for details

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.