ASP. net mvc ValidationAttribute server-side custom Verification

Source: Internet
Author: User

Client verification the above only describes the custom verification of the client, so that user input is not reliable enough, users can bypass our defined client verification. Therefore, client-only verification is not enough. We need to perform another verification on the server. ValidationAttribute server verification must inherit from ValidationAttribute and override the IsValid virtual method to customize its own verification rules. The ValidationAttribute declaration is roughly as follows: attribute {// message displayed when verification fails: public virtual string FormatErrorMessage (string name); // custom verification: protected virtual ValidationResult IsValid (object value, ValidationContext validationContext ); // custom verification 2 public virtual bool IsValid (object value);} UrlAttributeUrlAttrib Ute is used to verify the validity of the Url format. This feature has been implemented in NET Framework 4.5. Let's take an example. Public class Link {[Required] [DisplayName ("Text")] public string Text {get; set;} [Url] [Required] [DisplayName ("Url Link")] public string Url {get; set ;}} public class UrlAttribute: ValidationAttribute, IClientValidatable {public override string FormatErrorMessage (string name) {return string. format ("{0} Format error", name);} public UrlAttribute () {} public override bool IsValid (object value) {var Text = value as string; Uri uri; return (! String. isNullOrWhiteSpace (text) & Uri. tryCreate (text, UriKind. absolute, out uri);} public IEnumerable <ModelClientValidationRule> invoke (ModelMetadata metadata, ControllerContext context) {var validationRule = new ModelClientValidationRule {ErrorMessage = FormatErrorMessage (metadata. displayName), ValidationType = "url" ,}; yield return validationRule ;}} UrlAttribute implement Verification: the client only adds the data-val-url attribute to the input control, but its client verification is still valid because the validate plug-in has implemented its js verification script.

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.