asp.net-mvc-3 – 大于或等于今天日期验证注释在MVC3

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-3 – 大于或等于今天日期验证注释在MVC3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有人看到一个MVC3数据注释的日期验证,需要一个选定的日期等于或大于当前日期?

如果已经有第三方添加了很酷。我已经在使用DataAnnotationsExtensions,但不提供我正在寻找的内容

这似乎没有任何参考。所以,希望有人已经解决了这个问题,然后再尝试重新创造轮子,并写下自己的定制验证器。

我已经尝试了Range,但需要2个日期,并且都必须是字符串格式的常量,例如[Range(typeof(DateTime),“1/1/2011”,“1/1/2016”)]帮助。而DataAnnotationsExtensions Min验证器只接受int和double

更新已解决

感谢@BuildStarted这是我结束了,它的工作伟大的服务器端,现在客户端与我的脚本

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Web.Mvc;
  5.  
  6. namespace Web.Models.Validation {
  7.  
  8. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,AllowMultiple = false,Inherited = true)]
  9. public sealed class DateMustBeEqualOrGreaterThanCurrentDateValidation : ValidationAttribute,IClientValidatable {
  10.  
  11. private const string DefaultErrorMessage = "Date selected {0} must be on or after today";
  12.  
  13. public DateMustBeEqualOrGreaterThanCurrentDateValidation()
  14. : base(DefaultErrorMessage) {
  15. }
  16.  
  17. public override string FormatErrorMessage(string name) {
  18. return string.Format(DefaultErrorMessage,name);
  19. }
  20.  
  21. protected override ValidationResult IsValid(object value,ValidationContext validationContext) {
  22. var dateEntered = (DateTime)value;
  23. if (dateEntered < DateTime.Today) {
  24. var message = FormatErrorMessage(dateEntered.ToShortDateString());
  25. return new ValidationResult(message);
  26. }
  27. return null;
  28. }
  29.  
  30. public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata Metadata,ControllerContext context) {
  31. var rule = new ModelClientCustomDateValidationRule(FormatErrorMessage(Metadata.DisplayName));
  32. yield return rule;
  33. }
  34. }
  35.  
  36. public sealed class ModelClientCustomDateValidationRule : ModelClientValidationRule {
  37.  
  38. public ModelClientCustomDateValidationRule(string errorMessage) {
  39. ErrorMessage = errorMessage;
  40. ValidationType = "datemustbeequalorgreaterthancurrentdate";
  41. }
  42. }
  43. }

在我的模特儿

  1. [required]
  2. [DateMustBeEqualOrGreaterThanCurrentDate]
  3. public DateTime SomeDate { get; set; }

客户端脚本

  1. /// <reference path="jquery-1.7.2.js" />
  2.  
  3. jQuery.validator.addMethod("datemustbeequalorgreaterthancurrentdate",function (value,element,param) {
  4. var someDate = $("#SomeDate").val();
  5. var today;
  6. var currentDate = new Date();
  7. var year = currentDate.getYear();
  8. var month = currentDate.getMonth() + 1; // added +1 because javascript counts month from 0
  9. var day = currentDate.getDate();
  10. var hours = currentDate.getHours();
  11. var minutes = currentDate.getMinutes();
  12. var seconds = currentDate.getSeconds();
  13.  
  14. today = month + '/' + day + '/' + year + ' ' + hours + '.' + minutes + '.' + seconds;
  15.  
  16. if (someDate < today) {
  17. return false;
  18. }
  19. return true;
  20. });
  21.  
  22. jQuery.validator.unobtrusive.adapters.addBool("datemustbeequalorgreaterthancurrentdate");

解决方法

创建自定义属性
  1. public class CheckDateRangeAttribute: ValidationAttribute {
  2. protected override ValidationResult IsValid(object value,ValidationContext validationContext) {
  3. DateTime dt = (DateTime)value;
  4. if (dt >= DateTime.UtcNow) {
  5. return ValidationResult.Success;
  6. }
  7.  
  8. return new ValidationResult(ErrorMessage ?? "Make sure your date is >= than today");
  9. }
  10.  
  11. }

代码被注销袖口,以便修复任何错误:)

猜你在找的asp.Net相关文章