MVC Series Learning (11)-Validation

Source: Internet
Author: User

1. Learn about the powerful validation features in MVC with an example

1.1 Create a "basic" MVC project, because you want to use the validation of JS, and then write the code in a view, and the code in the model is as follows

The code in model

1.2. Start testing

1.3 Viewing source code

<!DOCTYPE HTML><HTML><Head>    <Metaname= "Viewport"content= "Width=device-width" />    <title>Add</title>    <Scriptsrc= "/scripts/jquery-1.8.2.js"></Script>    <Scriptsrc= "/scripts/jquery.validate.js"></Script>    <Scriptsrc= "/scripts/jquery.validate.unobtrusive.js"></Script></Head><Body><formAction= "/home/modify"Method= "POST">       <Table>           <TR>               <TD>Name</TD>               <TD><inputData-val= "true"Data-val-length= "Field name must be a string with a maximum length of 3. "Data-val-length-max= "3"ID= "SName"name= "SName"type= "text"value="" />               <spanclass= "Field-validation-valid"data-valmsg-for= "SName"Data-valmsg-replace= "true"></span>               </TD>           </TR>           <TR>               <TD><label for= "SAge">Age</label></TD>               <TD><inputclass= "Text-box single-line"Data-val= "true"Data-val-number= "field age must be a number. "Data-val-range= "This position fills >=1 and &lt;=99 value"Data-val-range-max= " the"Data-val-range-min= "1"ID= "SAge"name= "SAge"type= "text"value="" />               <spanclass= "Field-validation-valid"data-valmsg-for= "SAge"Data-valmsg-replace= "true"></span>               </TD>           </TR>           <TR>               <TD></TD>               <TD><inputtype= "Submit"value= "Submit"/></TD>           </TR>       </Table></form><!--Visual Studio Browser Link -<Scripttype= "Application/json"ID= "__browserlink_initializationdata">    {"AppName":"Firefox","RequestID":"DA5289DA760B44F8B8C56A9684E0A5BD"}</Script><Scripttype= "Text/javascript"src= "Http://localhost:6217/e3c73d9e3ac34dfa9df5109ec8422564/browserLink"Async= "Async"></Script><!--End Browser Link -</Body></HTML>

Originally, these text boxes themselves did not change, just a few more properties, when the introduction of the validation of the JS file, based on the attributes on the text box, add the corresponding JS operation. When the discovery condition is not satisfied, the form is not submitted to the server, which is actually done as follows. Set the OnSubmit property in the form to return a false so that the form is not submitted.

2. Talk about features

is a feature that modifies the sname attribute and some description of the attribute

3. Other

Some additional instructions for the DataType enumeration

MVC Series Learning (11)-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.