Form data verification Method (ii)--asp. NET background verification

Source: Internet
Author: User

Yesterday, I wrote about how to quickly implement the form data validation method in the foreground, today, then yesterday, the background of the implementation of data validation methods recorded. First of all, I'm using ASP. So the background verification method is also based on. NET MVC.

Well, gossip, I'm back to the chase.

Background implementation of form data validation method is also quite simple, the following see I step-by-step to do ha. (in order to simply explain how forms are validated, and to reduce other unnecessary content, I'm not involved in the database.) )

1. Create a new. NET MVC application

This step is not detailed today, there is time to write an article on how to create a new project and the implementation of simple additions and deletions of the blog, to consolidate their own, by the way to help and I like Montion chicken. To build the application, my name is FormCheck:

2. New User Information field Entity Data Class User816.cs

(The name of the people do not struggle with him, because I do not use the hot code just written, but the cold code written on August 16, because the evening time is limited, do not re-write)

As shown, I create a new User816.cs entity class in the Models folder, with the following code:

1 namespaceFormcheck.models2 {3      Public classUser8164     {5 6          Public stringUserName {Get;Set; }7 8          Public stringPassWord {Get;Set; }9 Ten          Public stringEMail {Get;Set; } One               A     } -}

Here we write three attributes, respectively, user name, password and mailbox, and the following will add a checksum method to these data.

The main additions [Required], [Stringlength], [regularexpression] Three bar, because these three are the most commonly used.

(1) Required

First look at the definition of this method:

This code only need to see can understand the Chinese character, yes, that is the "specified data field value is required", OK now we know what this means, then the question is, how does this thing use? See below:

   [Required (errormessage ="* required fields ")]     Public string Get set; }

Wrap the required in brackets above the attributes that need to be validated, and then add the error message. How, is not very simple, haha.

(2) Stringlength

This is the same, take a look at the meaning and usage of this thing, just a stroke of it:

Use the following:

[Stringlength (5, errormessage ="* exceeds the length of "publicstring  getset; }

The first parameter "5" above refers to the maximum length of the string, of course, here is not only set the maximum length, you can also set the minimum length, more than that, there are many other uses, here I do not elaborate, the specific need to use the students can search for information on the Internet.

(3) RegularExpression

That's a good one. Verify the regular expression:

Use the following:

[RegularExpression (@ "^[a-za-z0-9_.-]+@[a-za-z0-9-]+ (\.[ a-za-z0-9-]+) *\. [A-za-z0-9] {2,6}$"* Mailbox format error "// Regular expression public    Stringgetset; }

As for these verification methods, let's take a look at the actual usage.

3. Validating data

(1) Add the desired validation to the corresponding attribute:

The code looks like this:

namespaceformcheck.models{ Public classUser816 {[Stringlength (5, errormessage ="* More than the length")] [Required] Public stringUserName {Get;Set; } [Required (ErrorMessage="* Required Fields")] [Range ( -,1000000, errormessage ="* Number size out of range")]         Public stringPassWord {Get;Set; }        [Required] [RegularExpression (@"^[a-za-z0-9_.-]+@[a-za-z0-9-]+ (\.[ a-za-z0-9-]+) *\. [A-za-z0-9] {2,6}$", errormessage ="* Incorrect mailbox format")]//Regular Expressions         Public stringEMail {Get;Set; } }}

(2) Reference User816.cs on the front page

First, create a new method in HomeController, named GetInfo (), add the corresponding view, and then reference the User816:

(3) Create a new form

The code is as follows:

<div>@using (Html.BeginForm ("Getinfofunc","Home", FormMethod.Post)) {@Html. ValidationSummary (true)        <labelclass="Btn-default"> Name </label>@Html. Textboxfor (Model=model.username); @Html. Validationmessagefor (Model=model.username)<br> <labelclass="Btn-default"> Password </label>@Html. Textboxfor (Model=Model.password); @Html. Validationmessagefor (Model=Model.password)<br> <labelclass="Btn-default"> Email </label>@Html. Textboxfor (; @Html. Validationmessagefor (<buttonclass="Btn-info"Id="Submit"> Submit </button>    }</div>

In the above code, the main way to implement the background check function is this sentence oh, when used, remember not to leak:

@Html. validationmessagefor (model ==

Actually only need these three steps already can see the effect, but in order to guarantee a form to submit the demo completeness, I will make up the fourth step again, haha.

(4) Reception submission data, backstage reception

Ajax way to submit a form:

<script type="Text/javascript">$ (function () {$ ("#submit"). Click (function () {varUserName = $ ("#userName"). Val (); varPassWord = $ ("#passWord"). Val (); varEMail = $ ("#eMail"). Val (); $.ajax ({URL:"@Url. Action ("Getinfofunc", "Home")", type:"Post", data: {username:username, Password:password,                Email:email}, Success:function (data) {alert (data);        }            });    }); });</script>

Background creation Controller GETINFOFUNC () receives data:

[HttpPost] PublicActionResult Getinfofunc () {stringUserName = request["UserName"]; stringPassWord = request["PassWord"]; stringEMail = request["EMail"]; returnContent (UserName +","+ PassWord +","+eMail); }        

4. View Results

The effect here is similar to the way I wrote the front-end JS check yesterday, and it's very simple.

Well, today is written here, good study, day up, ah haha haha ha!



Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.