2012-12-03 42 views
1

我希望能夠驗證輸入的生日日期是18歲或更早。我搜查了許多關於此的帖子。不過,我不明白日期驗證mvc3導致問題

1)爲什麼MS沒有建成像其他的驗證比如字符串,電子郵件,密碼等 2)爲什麼當我得到的JavaScript到位,使這MVC3日期正確驗證,其他不顯眼的js不再工作。

我想有客戶端驗證還沒有提交之前,日期似乎並沒有與這個很好地工作。其餘的都是。

我錯過了什麼嗎?

一些代碼,我在我的模型

#1) [Display(Name = "Date of Birth (must be at least 18) ")] 
    public DateTime Birthdate 
    { 
     get 
     { 
      if ((SelectedBMonth != "0") && (SelectedBday != "0") && (SelectedBYear != "0")) 
       return DateTime.Parse(SelectedBMonth + "/" + SelectedBday + "/" + SelectedBYear); 
      else 
       return DateTime.MinValue; 
     } 
    } 

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) 
    { 
     if (Birthdate.Date > DateTime.Today.AddYears(-18)) 
      yield return new ValidationResult("You must be at least 18 years old to register", new[] { "Birthdate" }); 
    } 
#2) 
     [Required]   
    [CustomValidation(typeof(RegisterModel), "ValidateDOBDate")] 
    [DataType(DataType.Date)] 
    [Display(Name = "Date of Birth")]   
    public DateTime DateOfBirth { get; set; } 

    public static ValidationResult ValidateDOBDate(DateTime DateOfBirthtovalidate) { if (DateOfBirthtovalidate.Date > DateTime.Now.AddYears(-18)) { return new ValidationResult("User should be atleast 18 years old."); } if (DateOfBirthtovalidate.Date < DateTime.Now.AddYears(-150)) { return new ValidationResult("Please put a valid date"); } return ValidationResult.Success; } 

#3) 
     public class DateofBirthAttribute : ValidationAttribute, IClientValidatable 
    { 
     public override bool IsValid(object value) 
     { 
      if (value == null || (DateTime)value < DateTime.Today.AddYears(-18)) 
       return false; 

      return true; 
     } 

     public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) 
     { 
      yield return new ModelClientValidationRule 
      { 
       ErrorMessage = this.ErrorMessage, 
       ValidationType = "dateofbirth" 
      }; 
     } 
    } 

    [Required] 
    [Display(Name = "Date of Birth")] 
    [DateofBirth(ErrorMessage = "User must be at least 18")] 
    public DateTime Birthdate { get; set; } 

View : 

<div class="editor-label"> 
      @Html.LabelFor(m => m.Birthdate) 
     </div> 
     <div class="editor-field">     
     @Html.TextBoxFor(x => x.Birthdate) 
@Html.ValidationMessageFor(x => x.Birthdate) 
</div> 

Top of view before beginform: 
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"> </script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/MyScripts.js")" type="text/javascript"></script> 
<script type="text/javascript"> 
// we add a custom jquery validation method 
jQuery.validator.addMethod('greaterThan', function (value, element, params) { 
    if (!/Invalid|NaN/.test(new Date(value))) { 
     return new Date(value) > new Date($(params).val()); 
    } 
    return isNaN(value) && isNaN($(params).val()) || (parseFloat(value) > parseFloat($(params).val())); 
}, ''); 

// and an unobtrusive adapter 
jQuery.validator.unobtrusive.adapters.add('dateofbirth', {}, function (options) { 
    options.rules['greaterThan'] = true; 
    options.messages['greaterThan'] = options.message; 
}); 
</script> 
+0

能否請您提供您正在嘗試的代碼示例,並鏈接到你已經嘗試過的任何資源? –

+0

也許這是類似的東西,我不知道? [StackOverflow的示例](http://stackoverflow.com/questions/6250821/date-validation-using-asp-net-mvc-3-0) – gardarvalur

+0

你不需要任何參數,你可以比較生日與當前日期。 –

回答

0

這是一個動態驗證這是依賴於當前的日期試過了,它不是在默認的驗證選項。

您可以輕鬆地實現它,但是沒有產生任何客戶端驗證。

您必須JavaScript驗證也實現了客戶端。有關添加新的驗證到ASP.NET MVC看看這個ASP .Net MVC 3 unobtrusive custom client validation

富勒更多信息。

+0

我也嘗試過#3以上......但它似乎沒有做任何事情。 – DavieDave

+0

在客戶端還是服務器端? –

+0

在客戶端,我在這種情況下服務器端在一個點上工作。 – DavieDave