0

我对日期验证有 2 个具体要求。1) 日期字段应为必填项 2) 不接受无效日期。

当日期为空时,我想显示“需要日期”

当日期无效时,我想显示“日期无效”

所以请指导我如何做到这一点。

在这里我尝试编写代码但不知道它是否有效。

public class Student
    {
        [Required(ErrorMessage = "DOB require")]
        [Display(Name = "DOB :")]
        [DataType(DataType.Date)]
        public DateTime Dob { get; set; }

    }

帮助我在客户端实现这一点。谢谢

编辑

一个人以这种方式指导我为了验证日期格式,最好创建一个自定义 ValidationAttribute 如下所示

[AttributeUsage(AttributeTargets.Property, Inherited = false,
    AllowMultiple = false)]
public sealed class DateOnlyAttribute : ValidationAttribute
{
    public DateOnlyAttribute() :
        base("\"{0}\" must be a date without time portion.")
    {
    }

    public override bool IsValid(object value)
    {
        if (value != null)
        {
            if (value.GetType() == typeof(DateTime))
            {
                DateTime dateTime = (DateTime)value;
                return dateTime.TimeOfDay == TimeSpan.Zero;
            }
            else if (value.GetType() == typeof(Nullable<DateTime>))
            {
                DateTime? dateTime = (DateTime?)value;
                return !dateTime.HasValue
                    || dateTime.Value.TimeOfDay == TimeSpan.Zero;
            }
        }
        return true;
    }

    public override string FormatErrorMessage(string name)
    {
        return string.Format(CultureInfo.CurrentCulture,
            ErrorMessageString, name);
    }
}

然后您可以将其用作数据注释,如下所示

  [DateOnly]
public DateTime Dob { get; set; }

但是上面的代码对我来说不是很清楚,因为我是 MVC 的新手

这是关于上述代码的几个问题

这段代码不清楚

public override bool IsValid(object value)
    {
        if (value != null)
        {
            if (value.GetType() == typeof(DateTime))
            {
                DateTime dateTime = (DateTime)value;
                return dateTime.TimeOfDay == TimeSpan.Zero;
            }
            else if (value.GetType() == typeof(Nullable<DateTime>))
            {
                DateTime? dateTime = (DateTime?)value;
                return !dateTime.HasValue
                    || dateTime.Value.TimeOfDay == TimeSpan.Zero;
            }
        }
        return true;
    }

1)如果这是真的,那么将返回什么

if (value.GetType() == typeof(DateTime))

2)这条线会做什么

返回 dateTime.TimeOfDay == TimeSpan.Zero;

3)这条线是什么意思

else if (value.GetType() == typeof(Nullable))

4)下面的代码是什么意思

DateTime? dateTime = (DateTime?)value;
                return !dateTime.HasValue
                    || dateTime.Value.TimeOfDay == TimeSpan.Zero;

请详细指导我以了解您编写的代码。谢谢

4

1 回答 1

1

在需要客户端验证的 .cshtml 文件中包含以下代码,

<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>

编辑

您提出的最后 4 个问题的解释,

1)if (value.GetType() == typeof(DateTime))检查“值”(对象)的类型是否可以是日期时间!换句话说,它检查不可为空的日期格式。

2)return dateTime.TimeOfDay == TimeSpan.Zero;如果 'value' 具有有效的日期 TimeSpan,则返回 false。Zero 为零时间 (00:00:00) 返回 TimeSpan 注意:如果您不能将 'value' 转换为日期时间,则会得到等于 TimeSpan 的 'datetime' 值。零

3)else if (value.GetType() == typeof(Nullable)<DateTime>)与问题 1 相同的情况,但检查可为空的日期时间(注意:可空的日期时间和不可空的日期时间被视为不同的数据类型)

4)

    DateTime? dateTime = (DateTime?)value;
    return !dateTime.HasValue || dateTime.Value.TimeOfDay == TimeSpan.Zero;

与问题 2 相同的情况,如果您不能强制转换为可为空的 DateTime,则您的 'datetime' 等于 TimeSpan.Zero

于 2013-09-17T08:49:45.703 回答