简体   繁体   English

日期验证 - StartDate,EndDate MVC

[英]Date Validations - StartDate, EndDate MVC

I need to do validation on StartDate and EndDate 我需要在StartDate和EndDate上进行验证

Validations: 验证:

  • StartDate must be set value less than or equal to Endate. StartDate必须设置为小于或等于Endate的值。
  • EndDate must be set greater than or equal to startdate. 必须将EndDate设置为大于或等于startdate。

So far my code: 到目前为止我的代码:

object: 宾语:

    [DisplayName("Effective Start Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] 
    public DateTime EffectiveStartDate { get; set; }

    [DisplayName("Effective End Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] 
    public DateTime EffectiveEndDate { get; set; }

View: 视图:

   </tr>  
          <tr>
            <td class="lables"><%= Html.LabelFor(model => model.EffectiveEndDate)%></td>
            <td class="data" id = "endDate"><%= Html.EditorFor(model => model.EffectiveEndDate)%>
            <%= Html.ValidationMessageFor(model => model.EffectiveEndDate)%></td>
        </tr>  
          <tr>
            <td class="lables"><%= Html.LabelFor(model => model.ErrorCheckEnabled)%></td>
            <td class="data" ><%= Html.TextAreaFor(model => model.ErrorCheckEnabled)%>
             <%= Html.ValidationMessageFor(model => model.EffectiveEndDate)%></td>
        </tr> 

How should I go with validation. 我该如何进行验证。 Should I do on client site on 我应该在客户端网站上做什么

$("#frm").validate

[or] [要么]

??? ???

This must be said: You should always do a server-side validation, as the user can turn off javascript and thus your validation won't be applied. 必须这样说:您应该始终进行服务器端验证,因为用户可以关闭javascript,因此不会应用您的验证。 But I recommend making client-side validation as well so the UX will be nicer. 但我建议进行客户端验证,以便UX更好。 Server-side and client-side validation can easily exist simultaneously. 服务器端和客户端验证可以轻松同时存在。

For server-side you can easily create your own validation attribute by deriving from the ValidationAttribute class. 对于服务器端,您可以通过从ValidationAttribute类派生来轻松创建自己的验证属性。 Look at this example: 看看这个例子:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class EndDateAttribute : ValidationAttribute
{
    public EndDateAttribute(DateTime endDate)
    {
        EndDate = endDate;
    }

    public DateTime EndDate { get; set; }

    public override bool IsValid(object value)
    {
        if (value == null)
            return false;

        DateTime val;
        try
        {
            val = (DateTime)value;
        }
        catch (InvalidCastException)
        {
            return false;
        }

        if (val >= EndDate)
            return false;

        return true;
    }

}

You can probably figure out how to make StartDate. 您可以弄清楚如何制作StartDate。

UPDATE: Using this is quite simple. 更新:使用它非常简单。 You just have to apply it to your properties as any other (validation) attribute. 您只需将其作为任何其他(验证)属性应用于您的属性。 For example 例如

[DisplayName("Effective Start Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] 
[StartDate(DateTime.Now)]
public DateTime EffectiveStartDate { get; set; }

[DisplayName("Effective End Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] 
[EndDate(new DateTime(2011, 9, 24)]
public DateTime EffectiveEndDate { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM