MVC authentication 12-use dataannotationsextensions to validate integers, messages, minimums, file types, URL addresses, and more

Source: Internet
Author: User

Original: MVC authentication 12-use dataannotationsextensions to validate integers, messages, minimums, file types, URL addresses, and more

This article experiences from http://dataannotationsextensions.org/ DATAANNOTATIONSEXTENSIONS.MVC3, which is an extension of dataannotation, can perform two-step verification on both the client and the server, and the types that can be verified include:

Message Verification
Integral type Verification
Date validation
Digital validation (that is, zero-based numbers, excluding with decimal points)
Whether the same validation
File type validation
int type validation (can be negative int type)
Maximum numeric validation (values can be with decimal points, can be validated against negative numbers)
Minimum numeric validation (values can be with decimal points, can be validated against negative numbers)
Numeric validation (can be negative, can take decimal points, or cannot be a string)
URL address Verification
Year validation
......

MVC Authentication Brotherhood:
MVC authentication 01-BASIC, Remote authentication
MVC validation 02-Custom validation rules, message validation
MVC validation 03-Custom validation rules, forbidden to enter certain values
MVC validation 04-Custom validation rules, date range validation
MVC validation 05-Custom validation rules, validation of 2 attribute values
MVC validation 06-Custom error messages
MVC validation 07-Custom model level validation
MVC Validation 08-jquery Asynchronous validation
MVC validation 09-Using MVC's Ajax.beginform method to implement asynchronous validation
MVC authentication 10-How to implement the client service side double asynchronous authentication
MVC validation 11-using jquery asynchronous validation for complex types


-Installation

References → Right click → select "Manage nuget packages" → enter "dataannotationsextensions" search → install "dataannotationsextensions.mvc3"

2 files need to be introduced at the same time, dataannotationsextensions for server-side validation, DATAANNOTATIONSEXTENSIONS.MV3 User Client authentication:

When introduced, the reference folder includes:

In addition, do not be confused by the name of Dataannotationsextensions.mv3, under the MVC4 can still be used.

Verify message, Integer, minimum, file type

-view Model

   1:  using System.ComponentModel.DataAnnotations;
   2:  using dataannotationsextensions;
   3:  
   4:  namespace mvcapplication1.models
   5:  {
   6:  public    class Sample
   7:      {
   8:          "Please enter a valid mailbox")]
   9:          "required")]
E   :          "email")]
One   :  public        string Email {get; set;}
  :  
"Must be an integer")]          
"At least 1")]          
  :          "Age")]
  :  public        int age {get; set;}
  :  
  :          [Dataannotationsextensions.fileextensions ("png|jpg|jpeg|gif"" Allowed file type is Png|jpg|jpeg|gif ")]
  :          "file type")]
  :  public        string File {get; set;}
  :      }
  :  }
At   :  

-View: Must contain jquery associated with client validation, i.e. @scripts.render ("~/bundles/jqueryval")

   1:  @model MvcApplication1.Models.Sample
   2:  
   3:  @{
   4:      "Index";
   5:      "~/views/shared/_layout.cshtml";
   6:  }
   7:  
   8:  
   9:  
  :  @using (Html.BeginForm ()) {
One   :      @Html. ValidationSummary (true)
  :  
  :      <fieldset>
  :          <legend>Sample</legend>
  :  
  :          class="Editor-label">
  :              @Html. labelfor (model = model. Email)
  :          </div>
  :          class="Editor-field">
  :              @Html. editorfor (model = model. Email)
  :              @Html. validationmessagefor (model = model. Email)
  :          </div>
At   :  
  :          class="Editor-label">
  :              @Html. labelfor (model = model. Age)
  :          </div>
  :          class="Editor-field">
  :              @Html. editorfor (model = model. Age)
  £ º              @Html. validationmessagefor (model = model. Age)
  :          </div>
  £ º  
  :          class="Editor-label">
  :              @Html. labelfor (model = model. File)
  :          </div>
  :          class="Editor-field">
@Html. editorfor (model = model.               File)
  Panax notoginseng  :            @Html. validationmessagefor (model = model. File)
  :          </div>
  :  
Max   :          <p>
In   :              <input type="submit"value="Commit" />
  :          </p>
  :      </fieldset>
  :  }
  :  
  :  <div>
  :      @Html. ActionLink ("back to List""Index")
  :  </div>
  £ º  
  :  @section Scripts {
  Wuyi:      @Scripts. Render ("~/bundles/jqueryval")
  :  }
  :  


-Results

Mailbox does not meet the requirements of the error:

Age less than 1 error:

Age is not an integral type error:

File type does not meet the required error:

Verify URL address, compare validation

-view Model

   1:  public    class Sample1
   2:      {
   3:          "password")]
   4:  public        string Password {get; set;}
   5:  
   6:          "Confirm password")]
   7:          [Equalto ("Password""" Password Mismatch ")]
   8:  public        string passwordconfirm {get; set;}
   9:  
"URL address ")]          
One   :          "URL address does not meet the requirements"
  :  public        string homepage {get; set;}
  :      }

-View

   1:  @model MvcApplication1.Models.Sample1
   2:  
   3:  @{
   4:      "Hello";
   5:      "~/views/shared/_layout.cshtml";
   6:  }
   7:  
   8:  
   9:  
  :  @using (Html.BeginForm ()) {
One   :      @Html. ValidationSummary (true)
  :  
  :      <fieldset>
  :          <legend>Sample1</legend>
  :  
  :          class="Editor-label">
  :              @Html. labelfor (model = model. Password)
  :          </div>
  :          class="Editor-field">
  :              @Html. editorfor (model = model. Password)
  :              @Html. validationmessagefor (model = model. Password)
  :          </div>
At   :  
  :          class="Editor-label">
  :              @Html. labelfor (model = model. PASSWORDCONFIRM)
  :          </div>
  :          class="Editor-field">
  :              @Html. editorfor (model = model. PASSWORDCONFIRM)
  £ º              @Html. validationmessagefor (model = model. PASSWORDCONFIRM)
  :          </div>
  £ º  
  :          class="Editor-label">
  :              @Html. labelfor (model = model. Homepage)
  :          </div>
  :          class="Editor-field">
@Html. editorfor (model = model.               Homepage)
  Panax notoginseng  :            @Html. validationmessagefor (model = model. Homepage)
  :          </div>
  :  
Max   :          <p>
In   :              <input type="submit"value="Commit" />
  :          </p>
  :      </fieldset>
  :  }
  :  
  :  @section Scripts {
A   :      @Scripts. Render ("~/bundles/jqueryval")
  :  }
  £ º  


-Results

Inconsistent password error:

URL address does not meet the requirements:


Resources:
※introducing DATA ANNOTATIONS EXTENSIONS

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.